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?
– Tuxpilgrim
Yes, I’ve completed the question with what I’ve done so far :)
– Mi Tavares
By "connect the numbers buttons to the virtual lcd", you say link the events of the buttons to display values on the lcd?
– Tuxpilgrim
Yes, exactly
– Mi Tavares
You can use the event clicked of your
QPushButton
and each time click show something on your lcd using the methoddisplay()
. 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))
– Tuxpilgrim