【问题标题】:Connection between from QDialog to QMainWindow - PyQt5从 QDialog 到 QMainWindow 之间的连接 - PyQt5
【发布时间】:2018-02-25 15:50:23
【问题描述】:

我用qtdesignerPyQt5 创建了两个小部件(QMainWindow 作为 win_one 和 QDialog 作为 win_two)。 从win_one,我打开win_two,填写lineEdit,然后按OK,将条目转移到win_one中显示的label。除了两个问题,一切都很好:

  1. win_one 窗口打开为.showMaximized(),但填完标签后,窗口的尺寸发生了变化。
  2. win_one 中的按钮停止工作

front_win_one.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_win_one(object):
    def setupUi(self, win_one):
        win_one.setObjectName("win_one")
        win_one.resize(1147, 234)
        self.centralwidget = QtWidgets.QWidget(win_one)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(50, 50, 111, 51))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(40, 160, 131, 31))
        self.label.setObjectName("label")
        win_one.setCentralWidget(self.centralwidget)

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

    def retranslateUi(self, win_one):
        _translate = QtCore.QCoreApplication.translate
        win_one.setWindowTitle(_translate("win_one", "MainWindow"))
        self.pushButton.setText(_translate("win_one", "To qdialog"))
        self.label.setText(_translate("win_one", "TextLabel"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win_one = QtWidgets.QMainWindow()
    ui = Ui_win_one()
    ui.setupUi(win_one)
    win_one.show()
    sys.exit(app.exec_())

front_win_two.py

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_win_two(object):
    def setupUi(self, win_two):
        win_two.setObjectName("win_two")
        win_two.resize(317, 278)
        self.pushButton = QtWidgets.QPushButton(win_two)
        self.pushButton.setGeometry(QtCore.QRect(40, 120, 121, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtWidgets.QLineEdit(win_two)
        self.lineEdit.setGeometry(QtCore.QRect(30, 50, 161, 21))
        self.lineEdit.setObjectName("lineEdit")

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

    def retranslateUi(self, win_two):
        _translate = QtCore.QCoreApplication.translate
        win_two.setWindowTitle(_translate("win_two", "Dialog"))
        self.pushButton.setText(_translate("win_two", "OK"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    win_two = QtWidgets.QDialog()
    ui = Ui_win_two()
    ui.setupUi(win_two)
    win_two.show()
    sys.exit(app.exec_())

back.py

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QDialog
from front_win_1 import Ui_win_one
from front_win_2 import Ui_win_two

class win_two(QDialog, Ui_win_two):
    def __init__(self, parent=None):
        super(win_two, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.vers_main)

    def vers_main(self):
        entry = self.lineEdit.text()
        win_one().label.setText(entry)

class win_one(QMainWindow, Ui_win_one):
    def __init__(self, parent=None):
        super(win_one, self).__init__(parent)
        self.setupUi(dialog)
        self.pushButton.clicked.connect(self.open_qdialog)

    def open_qdialog(self):
        self.dialog_win_2 = win_two()
        self.dialog_win_2.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    dialog = QMainWindow()
    prog = win_one(dialog)
    dialog.showMaximized()
    sys.exit(app.exec_())

谢谢

【问题讨论】:

  • python27 还是 python3?无法使用 python35 重现错误 #1
  • 我已经用python测试过,我无法重现第一个错误。

标签: python pyqt pyqt5 qmainwindow qdialog


【解决方案1】:

您的代码有一些不一致之处:

  1. 你不应该这样做dialog = QMainWindow(),因为创建类win_one的对象就足够了,为此你必须将self.setupUi(dialog)更改为self.setupUi(self)

  2. 使用win_one().label.setText(entry) 语句,您正在创建一个新对象,这是不必要的,此外您正在丢失以前的对象,因此当您再次按下窗口时,QDialog 不会打开,一个简单的解决方案是将其作为父级传递给 win_one 到 win_two 并使用 self.parent() 函数访问它。

以上全部在以下部分实现:

class win_two(QDialog, Ui_win_two):
    def __init__(self, parent=None):
        super(win_two, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.vers_main)

    def vers_main(self):
        entry = self.lineEdit.text()
        self.parent().label.setText(entry)

class win_one(QMainWindow, Ui_win_one):
    def __init__(self, parent=None):
        super(win_one, self).__init__(parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.open_qdialog)

    def open_qdialog(self):
        self.dialog_win_2 = win_two(self)
        self.dialog_win_2.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    prog = win_one()
    prog.showMaximized()
    sys.exit(app.exec_())

注意:我永远无法重现第一个错误,只能重现第二个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多