从qdialog到qmainwindow -pyqt5之间的连接



我用 qtdesignerPyQt5创建了两个小部件( QMainWindow为win_one和 QDialog)。从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_())

谢谢

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

  1. 您不应该执行此dialog = QMainWindow(),因为它足以创建类Win_One的对象,为此,您必须将self.setupUi(dialog)更改为self.setupUi(self)

  2. 使用语句win_one().label.setText(entry),您正在创建一个新对象,这是不必要的,除了您失去了先前的对象,因此当您再次按下窗口时,不打开QDialog,一个简单的解决方案就是将其传递给父母win_one to 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_())

注意:我永远无法复制第一个错误,仅次于第二个错误。

相关内容

  • 没有找到相关文章

最新更新