首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在qt中使用简单的python脚本(通过QT-designer)

在qt中使用简单的python脚本(通过QT-designer)
EN

Stack Overflow用户
提问于 2013-07-21 03:39:48
回答 1查看 1.6K关注 0票数 0

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

这是我在QT-designer中制作的没有修改脚本的qt GUI。谢谢你的帮助!

代码语言:javascript
复制
# -*- 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_())
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-21 03:49:58

我想我明白你的问题,但我不能完全肯定。无论如何,我认为你想要做的事情是这样的。首先,不是清除标签,而是将其替换为以下内容:

代码语言:javascript
复制
QtCore.QObject.connect([...], self.button_clicked)

现在,您需要在类中创建该函数:

代码语言:javascript
复制
def button_clicked(self):
    x = 100 # Generate your number here
    self.label.setText("My number: {0}".format(x))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17765988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档