在qt中使用简单的python脚本(通过QT设计器)

时间:2013-07-20 19:39:48

标签: python qt-designer

我是一名python和qt设计师的新手用户。我已经为随机生成数字做了一个简单的python代码。现在我想要的是当我点击一个按钮时,输出将在GUI上,而不是在终端上。到目前为止,我已经设法点击一个按钮,输出将在终端生成,但这不是我想要的。

所以这是我在QT设计器中制作的qt GUI,没有对脚本进行修改。谢谢你的帮助!

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'qt_hello.ui'
#
# Created: Sat Jul 20 21:22:41 2013
#      by: PyQt4 UI code generator 4.10.2
#
# WARNING! All changes made in this file will be lost!

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.pushButton = QtGui.QPushButton(Form)
    self.pushButton.setGeometry(QtCore.QRect(210, 230, 95, 23))
    self.pushButton.setObjectName(_fromUtf8("pushButton"))
    self.label = QtGui.QLabel(Form)
    self.label.setGeometry(QtCore.QRect(80, 30, 121, 71))
    self.label.setObjectName(_fromUtf8("label"))

    self.retranslateUi(Form)
    QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), self.label.clear)
    QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUi(self, Form):
    Form.setWindowTitle(_translate("Form", "Form", None))
    self.pushButton.setText(_translate("Form", "PushButton", None))
    self.label.setText(_translate("Form", "TextLabel", 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_())

1 个答案:

答案 0 :(得分:0)

我想我理解你的问题,但我不完全确定。无论如何,我认为你想要做的就是这样。首先,不要清除标签,而是将其替换为:

QtCore.QObject.connect([...], self.button_clicked)

现在,您需要在班级中创建该功能:

def button_clicked(self):
    x = 100 # Generate your number here
    self.label.setText("My number: {0}".format(x))