Im just starting out making GUI applications and Im writing a hangman game in python
What I cant figure out is how to connect button clicks to stuff other than just clear() (in the code there is a section that reads self.label.clear) but I'd like to for example make the text label display 'a' if I clicked the button or run a function I have had the button run a function but the results only show up on the terminal
How can I make the label read 'a' by clicking the button?
Also, Is Qt4 designer regarded as a 'bad' GUI program? All results to questions I've googled regarding python and GUI seem to have a code not that similar to the code generated from Qt4?
Here is the code freshly generated from Qt4, before I butchered it for hours
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig,_encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.Button = QtGui.QPushButton(Form)
self.Button.setGeometry(QtCore.QRect(80, 180, 85, 27))
self.Button.setObjectName(_fromUtf8("Button"))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(120, 50, 58, 15))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.label.clear)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.Button.setText(_translate("Form", "a", None))
self.label.setText(_translate("Form", "okaiokai", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
Form = QtGui.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
you can define a new method to receive clicked and process.
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(400, 300)
self.Button = QtGui.QPushButton(Form)
self.Button.setGeometry(QtCore.QRect(80, 180, 85, 27))
self.Button.setObjectName(_fromUtf8("Button"))
self.label = QtGui.QLabel(Form)
self.label.setGeometry(QtCore.QRect(120, 50, 58, 15))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Form)
QtCore.QObject.connect(self.Button, QtCore.SIGNAL(_fromUtf8("clicked()")), self.setLabel)
QtCore.QMetaObject.connectSlotsByName(Form)
def setLabel(self):
self.label.setText(self.Button.text())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.