0

I am a python and qt-designer newbie user. I have made a simple python code for random generating numbers. Now what i want is when i click a button the output will be on GUI and not on terminal. So far i have managed to click a button and the output will be generated in terminal but that is not what i want.

So here is the qt GUI which i have made in QT-designer with no modification as to script. Thanks for any help!

# -*- 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_())
4

1 回答 1

0

I think I understand your question, but I'm not entirely certain. Anyway, I think what you want to do is something like this. First, instead of clearing the label, replace it with something like:

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

Now, you need to create that function in your class:

def button_clicked(self):
    x = 100 # Generate your number here
    self.label.setText("My number: {0}".format(x))
于 2013-07-20T19:49:58.483 回答