Connect a button with a lcd screen pyqt4

0

Good afternoon everyone!

I need to develop a calculator in pyqt 4, but I do not know how to connect the number / operations buttons to the virtual lcd display. I already researched, but found nothing that solved my problem. Can someone help me? What I've done so far is here (excuse me for the code rolled up, I'm an inciter!

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()
    
asked by anonymous 13.08.2018 / 19:12

1 answer

0

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

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

  • I will define a function (very simple, just for a better understanding), to receive the msg that 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 only to call the function that displays in exibe_no_lcd() :

    def botao_um_clicado():
         exibe_no_lcd("Eu sou o numero 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 using the clicked event;

    • No SOen has a question about using display() ;

    • Still in SOen has a question about calling a function by clicking a QPushButton .

    13.08.2018 / 21:22