代码仅在PDB处于活动状态时有效

时间:2018-06-11 19:40:25

标签: python pyqt pyqt5 pdb

我目前正在尝试创建一个可以与对话窗口交互的主窗口,但我遇到了一个我认为很有趣的问题。

基本上,我正试图让test_1test_2进行互动。当我自己测试代码,工作并执行它应该做的事情时,但是当我使用Test_1Test_2交互时,关于测试2的脚本不再具有其完整功能。

有趣的是,当我浏览代码时,它能够打印"hello",并且我链接到print("hello")的函数不会绑定到类。但是,不起作用的函数(我只是要求在文本框中插入“hello”的函数)与一个类绑定,这是一个无效的函数。此外,如果我在pdb。{/ p>中使用Test_1,代码也可以正常运行

这是关于测试1和测试2的代码(如果你还没有安装PyQT5,你可能需要安装):

TEST_1

import test_2 as t2
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Test_1(object):
    def setupUi(self, Test_1):
        Test_1.setObjectName("Test_1")
        Test_1.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(Test_1)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(130, 120, 461, 281))
        self.pushButton.setObjectName("pushButton")
        Test_1.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(Test_1)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        Test_1.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(Test_1)
        self.statusbar.setObjectName("statusbar")
        Test_1.setStatusBar(self.statusbar)

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


        self.pushButton.clicked.connect(self.connect_test_2)



    def retranslateUi(self, Test_1):
        _translate = QtCore.QCoreApplication.translate
        Test_1.setWindowTitle(_translate("Test_1", "MainWindow"))
        self.pushButton.setText(_translate("Test_1", "Open Dialog"))

    def connect_test_2(self):
        #import pdb; pdb.set_trace()
        self.t2 = QtWidgets.QDialog()
        ui = t2.Ui_Test_2()
        ui.setupUi(self.t2)
        self.t2.show()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Test_1()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

测试2

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Test_2(object):
    def setupUi(self, Test_2):
        Test_2.setObjectName("Test_2")
        Test_2.resize(400, 300)
        self.buttonBox = QtWidgets.QDialogButtonBox(Test_2)
        self.buttonBox.setGeometry(QtCore.QRect(290, 20, 81, 241))
        self.buttonBox.setOrientation(QtCore.Qt.Vertical)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.pushButton = QtWidgets.QPushButton(Test_2)
        self.pushButton.setGeometry(QtCore.QRect(20, 90, 341, 161))
        self.pushButton.setObjectName("pushButton")
        self.textEdit = QtWidgets.QTextEdit(Test_2)
        self.textEdit.setGeometry(QtCore.QRect(20, 10, 104, 71))
        self.textEdit.setObjectName("textEdit")

        self.retranslateUi(Test_2)

        #self.buttonBox.accepted.connect(Test_2.accept)
        #self.buttonBox.rejected.connect(Test_2.reject)

        QtCore.QMetaObject.connectSlotsByName(Test_2)
        self.pushButton.clicked.connect(self.printHello)
        self.pushButton.clicked.connect(dummyCode)


    def printHello(self):
        self.textEdit.setText("Hello")


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

def dummyCode():
    print("Hello")

if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_Test_2()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

0 个答案:

没有答案