在程序运行 PyQt 时添加标签
Posted
技术标签:
【中文标题】在程序运行 PyQt 时添加标签【英文标题】:Add label while program running PyQt 【发布时间】:2016-11-01 00:06:03 【问题描述】:我刚刚开始使用 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_())
它什么也没做。我想添加一个标签,但我找不到如何做到这一点,如果有人可以帮助我,我将不胜感激。我的 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))
【问题讨论】:
添加后有没有尝试调用label.show()
?
哇!我以前试过,但不知何故没有用,现在可以了!非常感谢。
如果小部件是在显示父级之前创建的,则它们将与它们的父级一起显示。之后你需要手动调用show。这允许您创建小部件并按需显示它们。不知道你对设计师不使用布局的意思是什么,因为这是一个通常用它做的事情。
【参考方案1】:
您应该在 GUI 描述中添加标签:
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"))
self.label = QtGui.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(80, 400, 371, 71))
font = QtGui.QFont()
font.setPointSize(16)
self.label.setFont(font)
self.label.setStyleSheet(_fromUtf8("color:rgb(255, 85, 127)"))
self.label.setObjectName(_fromUtf8("label"))
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))
然后在您的 def send(self): 中为该标签设置一个文本(假设您的 GUI 文件名为“GUI.py”:
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_())
【讨论】:
以上是关于在程序运行 PyQt 时添加标签的主要内容,如果未能解决你的问题,请参考以下文章