我正在使用PyQt5在Python GUI上工作,但出现错误“ AttributeError:'MainWindow'对象没有属性'cbWONum'”。如果我从QtDesigner导入.ui文件,则可以使其正常运行。但是,当我遇到此问题时,就要学会在Pycharm中创建自己的GUI。如果您可以查看我的代码并就我还是Python的新手给我做一个解释。感谢您的所有帮助。
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from Controller import CustOrders as CO
import DBConnection as DB
import time
import sys
class MainWindow(QMainWindow):
listofCustOrders = []
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("Paint Rack Log")
cbCONum = QComboBox()
self.setCentralWidget(cbCONum)
self.gUI()
def gUI(self):
global listofCustOrders
listofCustOrders = CO.CustOrders.getCustOrders()
for x in listofCustOrders:
self.cbCONum.addItem(x.getCustOrderNO())
cbCONum.currentIndexChanged.connect(self.coSelected)
def coSelected(self, text):
cbCOIndex = self.cbCONum.currentIndex()
selectedCO = listofCustOrders[cbCOIndex]
def main():
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
跟踪如下:
Traceback (most recent call last):
File "C:/Users/Documents/PyProjects/Launcher.py", line 51, in <module>
main()
File "C:/Users/Documents/PyProjects/Launcher.py", line 45, in main
window = MainWindow()
File "C:/Users/Documents/PyProjects/Launcher.py", line 23, in __init__
self.gUI()
File "C:/Users/Documents/PyProjects/Launcher.py", line 33, in gUI
self.cbCONum.addItem(x.getCustOrderNO())
AttributeError: 'MainWindow' object has no attribute 'cbCONum'