无法从PyQt5的另一个窗口访问功能

时间:2018-12-29 08:36:59

标签: python pyqt pyqt5

我制作了两个Windows:billing.ui和addCustomer.ui。 billing.ui中有一个按钮,该按钮应提供添加新客户的功能。为此,我使用了addCustomer.ui对话框。问题是当我在python文件中调用addCustomer时,无法从中访问函数。

我试图在堆栈溢出时寻找已经存在的答案,但文件的结构似乎与其他文件不同。

这是我的源代码 billing.py:

import os
from PyQt5 import QtGui, uic, QtWidgets
from PyQt5.QtWidgets import QInputDialog
from functools import partial
from addCustomer import addCustomerDialog

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "UI FILES/billing.ui"))

class billingWindow(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        self.addContact.clicked.connect(self.addCustomer)
        #addContact is name of button
    def addCustomer(self):

        #these 4 lines open new page
        self.newWindow = QtWidgets.QDialog()
        self.ui = addCustomerDialog()
        self.ui.setupUi(self.newWindow)
        self.newWindow.show()



if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)

    w = billingWindow()
    w.show()
    sys.exit(app.exec_())

addCustomer.py:

import os
from PyQt5 import QtGui, uic, QtWidgets
from functools import partial
import rec_rc

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "UI FILES/addCustomer.ui"))

class addCustomerDialog(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)

        self.okButton.clicked.connect(self.close)
        #this code is not running when called from billing.py

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)

    w = addCustomerDialog()
    w.show()
    sys.exit(app.exec_())

我想要代码

self.okButton.clicked.connect(self.close)

可以从billing.py调用时运行。 当我刚运行addCustomer.py时,代码运行良好。

如果您对我的问题有任何疑问,请发表评论,如果您投反对票,请提出原因。谢谢

0 个答案:

没有答案