[英]connect button to label - python & Qt4 designer
Im just starting out making GUI applications and Im writing a hangman game in python 我刚刚开始制作GUI应用程序,然后用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 我不能弄清楚的是如何将按钮单击连接到除clear()之外的其他东西(在代码中有一段读取self.label.clear),但是我想例如使文本标签显示为'a '如果我单击按钮或运行功能,则让按钮运行功能,但结果仅显示在终端上
How can I make the label read 'a' by clicking the button? 单击按钮如何使标签读为“ a”?
Also, Is Qt4 designer regarded as a 'bad' GUI program? 此外,Qt4设计器是否被视为“不良” GUI程序? 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?
我搜索过有关python和GUI的所有问题的结果似乎都具有与Qt4生成的代码不太相似的代码?
Here is the code freshly generated from Qt4, before I butchered it for hours 这是我花了几个小时才刚从Qt4生成的代码
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.