python - PyQt5 button to run function and update LCD -
i getting started creating gui's in pyqt5 python 3. @ click of button want run "randomint" function , display returned integer qlcdnumber named "lcd".
here's code:
import sys pyqt5.qtwidgets import qapplication, qwidget, qvboxlayout, qpushbutton, qlcdnumber random import randint class window(qwidget): def __init__(self): super().__init__() self.initui() def initui(self): lcd = qlcdnumber(self) button = qpushbutton('generate', self) button.resize(button.sizehint()) layout = qvboxlayout() layout.addwidget(lcd) layout.addwidget(button) self.setlayout(layout) button.clicked.connect(lcd.display(self.randomint())) self.setgeometry(300, 500, 250, 150) self.setwindowtitle('rand integer') self.show() def randomint(self): random = randint(2, 99) return random if __name__ == '__main__': app = qapplication(sys.argv) ex = window() sys.exit(app.exec_())
i getting output:
typeerror: argument 1 has unexpected type 'nonetype'
how can lcd display output function "randomint"?
the problem button.clicked.connect
expects slot (python callable object), lcd.display
returns none
. need simple function (slot) button.clicked.connect
display newly generated value. working version:
import sys pyqt5.qtwidgets import qapplication, qwidget, qvboxlayout, qpushbutton, qlcdnumber random import randint class window(qwidget): def __init__(self): super().__init__() self.initui() def initui(self): self.lcd = qlcdnumber(self) button = qpushbutton('generate', self) button.resize(button.sizehint()) layout = qvboxlayout() layout.addwidget(self.lcd) layout.addwidget(button) self.setlayout(layout) button.clicked.connect(self.handlebutton) self.setgeometry(300, 500, 250, 150) self.setwindowtitle('rand integer') self.show() def handlebutton(self): self.lcd.display(self.randomint()) def randomint(self): random = randint(2, 99) return random if __name__ == '__main__': app = qapplication(sys.argv) ex = window() sys.exit(app.exec_())
Comments
Post a Comment