程序运行PyQt时添加标签

时间:2016-11-01 00:06:03

标签: python qt user-interface qt-designer

我刚刚开始使用Qt Designer for python,我设计了一个主窗口。我想在点击按钮时添加一个标签。我找到了一个说使用布局的服务器,但似乎Qt Designer似乎没有使用它。我试着这个:

from main_ui import *
import sys
def send():
        label=QtGui.QLabel(myapp.ui.centralwidget)
        label.setGeometry(QtCore.QRect(10, 10, 46, 13))
        label.setText(myapp.ui.lineEdit.text())


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s
app = QtGui.QApplication(sys.argv)
myapp = StartQT4()
myapp.show()
QtCore.QObject.connect(myapp.ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), send)

QtCore.QMetaObject.connectSlotsByName(myapp)

sys.exit(app.exec_())

它没有做任何事情。我想添加一个标签,我无法找到如何做到这一点,如果somone可以帮助我,我会感激。我的ui文件是:

from PyQt4 import QtCore, QtGui
import time

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 StartQT4(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(767, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(230, 300, 75, 23))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(80, 330, 371, 71))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 767, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        self.scrollArea = QtGui.QScrollArea(self.centralwidget)
        self.scrollArea.setGeometry(QtCore.QRect(130, 70, 301, 191))
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName(_fromUtf8("scrollArea"))
        self.scrollAreaWidgetContents = QtGui.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 299, 189))
        self.scrollAreaWidgetContents.setObjectName(_fromUtf8("scrollAreaWidgetContents"))
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        p= QtGui.QPalette()
        p.setColor(QtGui.QPalette.Background, QtCore.Qt.white)
        self.scrollArea.setAutoFillBackground(True)
        self.scrollArea.setPalette(p)
        self.retranslateUi(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.pushButton.setText(_translate("MainWindow", "send", None))

1 个答案:

答案 0 :(得分:0)

您应该在GUI描述中添加标签:

from GUI import Ui_MainWindow
import sys
from PyQt4 import QtCore, QtGui


try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class MAIN(Ui_MainWindow):
    def send(self):
        print ui.lineEdit.text()
        ui.label.setText(ui.lineEdit.text())
        app.processEvents()


app = QtGui.QApplication(sys.argv)
Dialog = QtGui.QMainWindow()
ui = MAIN()
ui.setupUi(Dialog)
print dir(Dialog)

QtCore.QObject.connect(ui.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), ui.send)

QtCore.QMetaObject.connectSlotsByName(Dialog)

Dialog.show()
sys.exit(app.exec_())

然后在 def send(self):中为此标签设置一个文本(假设您的GUI文件名为“GUI.py”:

public boolean isAvailable(Context context, Intent intent) {
    final PackageManager mgr = context.getPackageManager();
    List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}