簡體   English   中英

將按鈕連接到標簽-python&Qt4 Designer

[英]connect button to label - python & Qt4 designer

我剛剛開始制作GUI應用程序,然后用python編寫了子手游戲

我不能弄清楚的是如何將按鈕單擊連接到除clear()之外的其他東西(在代碼中有一段讀取self.label.clear),但是我想例如使文本標簽顯示為'a '如果我單擊按鈕或運行功能,則讓按鈕運行功能,但結果僅顯示在終端上

單擊按鈕如何使標簽讀為“ a”?

此外,Qt4設計器是否被視為“不良” GUI程序? 我搜索過有關python和GUI的所有問題的結果似乎都具有與Qt4生成的代碼不太相似的代碼?

這是我花了幾個小時才剛從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_())

您可以定義一種新方法來接收點擊和處理。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM