Connect a button with a pyqt4 lcd screen

Asked

Viewed 35 times

0

Good afternoon, everyone!

I need to develop a calculator in pyqt 4, but I don’t know how to connect the number/operations buttons to the virtual lcd display. I have already researched, but I haven’t found anything to solve my problem. Can anyone help me? What I have done so far is here (forgive me for the coiled code, I am inciante!!

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

def __init__(self):
    super(Example, self).__init__()

    self.initUI()

def initUI(self):

   grid = QtGui.QGridLayout()
   self.setLayout(grid)

   lcd = QtGui.QLCDNumber(self)
   grid.addWidget(lcd, 0,1)

   um = QtGui.QPushButton('1', self)
   grid.addWidget(um, 1,1)
   dois = QtGui.QPushButton('2', self)
   grid.addWidget(dois, 1,2)
   tres = QtGui.QPushButton('3', self)
   grid.addWidget(tres, 1,3)
   quatro = QtGui.QPushButton('4', self)
   grid.addWidget(quatro, 2,1)
   cinco = QtGui.QPushButton('5', self)
   grid.addWidget(cinco, 2,2)
   seis = QtGui.QPushButton('6', self)
   grid.addWidget(seis, 2,3)
   sete = QtGui.QPushButton('7', self)
   grid.addWidget(sete, 3,1)
   oito = QtGui.QPushButton('8', self)
   grid.addWidget(oito, 3,2)
   nove = QtGui.QPushButton('9', self)
   grid.addWidget(nove, 3,3)
   zero= QtGui.QPushButton('0', self)
   grid.addWidget(zero, 4,1)
   mais= QtGui.QPushButton('+', self)
   grid.addWidget(mais, 4,2)
   menos= QtGui.QPushButton('-', self)
   grid.addWidget(menos, 4,3)
   vezes= QtGui.QPushButton('*', self)
   grid.addWidget(vezes, 5,1)
   dividir= QtGui.QPushButton('/', self)
   grid.addWidget(dividir, 5,2)
   igual= QtGui.QPushButton('=', self)
   grid.addWidget(igual, 5,3)

   self.move(300, 150)
   self.setWindowTitle('Calculadora')
   self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
  • There’s some code of what you’ve done?

  • Yes, I’ve completed the question with what I’ve done so far :)

  • By "connect the numbers buttons to the virtual lcd", you say link the events of the buttons to display values on the lcd?

  • Yes, exactly

  • You can use the event clicked of your QPushButton and each time click show something on your lcd using the method display(). Set a function that displays something on the lcd, and call it when the button is clicked, with: sete.clicked.connect(exibe_no_lcd(lcd,valor))

1 answer

0

You can do this by capturing the event of QPushButton the moment it is clicked. The event in question is the clicked.

Ex: Assuming we want to display a message on lcd when the button um is clicked.

  1. I will define a function( very simple, just for a better understanding), to receive the msg which will be shown on screen at runtime:

    def exibe_no_lcd(msg): #chama o metodo display() para exibir no lcd lcd.display(msg)

And a function just to call the function it displays in exibe_no_lcd():

def botao_um_clicado():
     exibe_no_lcd("Eu sou o numero 1!")
  1. Now we link the function that will be called when the button is clicked:

    um.clicked.connect(botao_um_clicado)

References:

  • In the wiki has a simple tutorial on the use of the event clicked;

  • In the Soen has a question about the use of display();

  • Still in the Soen has a question about calling a function by clicking on a QPushButton.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.