如何使用PyQt触发窗口小部件中的文本

时间:2015-05-24 15:11:52

标签: pyqt4

这可能看起来像一个很长的问题,但它实际上很短,我只是决定在这里复制所有工作代码。 我有一个主窗口和每日提示小部件。 我使用PyQt Designer生成了两个UI。 我可以从主窗口菜单中打开每日提示小部件,但我无法使按钮工作: 当单击上一个和下一个按钮时,我想替换每日提示小部件中的一些文本。

我有以下主窗口MainWindow.py

from PyQt4 import QtCore, QtGui
from MainWindowUi import Ui_MainWindow
from FormUi import Ui_Form

class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        # Main window user interface elements
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # Main window signal/slot connections
        self.setupConnections()

    @QtCore.pyqtSlot()
    def showTipDialog(self):
        '''Trig dialog Tip'''
        form = QtGui.QDialog()
        form.ui = Ui_Form()
        form.ui.setupUi(form)
        form.exec_()

    def setupConnections(self):
        '''Signal and Slot Support'''
        self.connect(self.ui.actionTip_of_the_Day, QtCore.SIGNAL('triggered()'), self.showTipDialog)

我有以下main.py

import sys

from PyQt4 import QtGui
from MainWindow import MainWindow

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

我有以下主窗口UI MainWindowUi.py

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

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created: Thu May 21 20:26:31 2015
#      by: PyQt4 UI code generator 4.11.3
#
# 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_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(800, 600)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        self.menuHelp = QtGui.QMenu(self.menubar)
        self.menuHelp.setObjectName(_fromUtf8("menuHelp"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)
        self.actionTip_of_the_Day = QtGui.QAction(MainWindow)
        self.actionTip_of_the_Day.setObjectName(_fromUtf8("actionTip_of_the_Day"))
        self.menuHelp.addAction(self.actionTip_of_the_Day)
        self.menubar.addAction(self.menuHelp.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
        self.menuHelp.setTitle(_translate("MainWindow", "Help", None))
        self.actionTip_of_the_Day.setText(_translate("MainWindow", "Tip of the Day", None))

我有以下小部件表单UI FormUi.py

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

# Form implementation generated from reading ui file 'Form.ui'
#
# Created: Thu May 21 23:57:41 2015
#      by: PyQt4 UI code generator 4.11.3
#
# 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(418, 249)
        self.verticalLayout = QtGui.QVBoxLayout(Form)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.verticalLayout.addWidget(self.lineEdit)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        self.pushButton_previous = QtGui.QPushButton(Form)
        self.pushButton_previous.setObjectName(_fromUtf8("pushButton_previous"))
        self.horizontalLayout.addWidget(self.pushButton_previous)
        self.pushButton_next = QtGui.QPushButton(Form)
        self.pushButton_next.setObjectName(_fromUtf8("pushButton_next"))
        self.horizontalLayout.addWidget(self.pushButton_next)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        Form.setWindowTitle(_translate("Form", "Form", None))
        self.lineEdit.setText(_translate("Form", "Here a tip I'd like to replace by pressing the buttons below.", None))
        self.pushButton_previous.setText(_translate("Form", "Previous Tip", None))
        self.pushButton_next.setText(_translate("Form", "Next Tip", None))

请运行main.py以打开主窗口,然后点击帮助> 每日提示以打开小部件。 感谢您的关注。

1 个答案:

答案 0 :(得分:0)

您有一个MainWindow类(您从main()函数实例化),您已编写该类以实例化您的Ui_MainWindow类(从而创建GUI)并将按钮链接到弹出对话框的方法。

现在只需对对话框应用相同的逻辑。而不是直接在QDialog()中创建showTipDialog,而是实例化子类QDialog。以与您为MainWindow所做的相似的方式编写子类。将来自上一个/下一个按钮的clicked信号连接到改变QLineEdit内容的适当方法(您编写的)。