我有以下代码:
import sys
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QPushButton,
QComboBox, QCompleter
from PyQt5.QtCore import QSize, Qt, QSortFilterProxyModel, QStringListModel
from PyQt5.QtGui import QIcon
import pandas as pd
import pickle
class ExtendedComboBox(QComboBox):
def __init__(self, parent=None):
super(ExtendedComboBox, self).__init__(parent)
self.setFocusPolicy(Qt.StrongFocus)
self.setEditable(True)
# add a filter model to filter matching items
self.pFilterModel = QSortFilterProxyModel(self)
self.pFilterModel.setFilterCaseSensitivity(Qt.CaseInsensitive)
self.pFilterModel.setSourceModel(self.model())
# add a completer, which uses the filter model
self.completer = QCompleter(self.pFilterModel, self)
# always show all (filtered) completions
self.completer.setCompletionMode(QCompleter.UnfilteredPopupCompletion)
self.setCompleter(self.completer)
# connect signals
self.lineEdit().textEdited.connect(self.pFilterModel.setFilterFixedString)
self.completer.activated.connect(self.on_completer_activated)
# on selection of an item from the completer, select the corresponding item from combobox
def on_completer_activated(self, text):
if text:
index = self.findText(text)
self.setCurrentIndex(index)
self.activated[str].emit(self.itemText(index))
# on model change, update the models of the filter and completer as well
def setModel(self, model):
super(ExtendedComboBox, self).setModel(model)
self.pFilterModel.setSourceModel(model)
self.completer.setModel(self.pFilterModel)
# on model column change, update the model column of the filter and completer as well
def setModelColumn(self, column):
self.completer.setCompletionColumn(column)
self.pFilterModel.setFilterKeyColumn(column)
super(ExtendedComboBox, self).setModelColumn(column)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('Tennis Form')
self.setWindowIcon(QIcon('C:/Users/XXX/Desktop/Project-Tennis/Tennis-icon.png'))
button1 = QPushButton("ATP", self)
button1.resize(350,50)
button1.clicked.connect(self.clickMethod1)
button1.move(225, 200)
button2 = QPushButton("WTA", self)
button2.resize(350,50)
button2.clicked.connect(self.clickMethod2)
button2.move(225, 275)
self.select = Select_player(self)
def clickMethod1(self):
import pandas as pd
import pickle
df = pd.read_pickle('C:/Users/XXX/Desktop/Project-Tennis/ATP_Rankings.pickle')
self.my_list = df["Name"].tolist()
self.select.show()
def clickMethod2(self):
import pandas as pd
import pickle
df = pd.read_pickle('C:/Users/XXX/Desktop/Project-Tennis/WTA_Rankings.pickle')
self.my_list = df["Name"].tolist()
self.select.show()
class Select_player(QMainWindow):
def __init__(self, parent=None):
super(Select_player,self).__init__()
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('Select Players')
self.setWindowIcon(QIcon('C:/Users/XXX/Desktop/Project-Tennis/Tennis-icon.png'))
self.player1 = ExtendedComboBox(self)
self.player1.resize(350,25)
self.player1.move(225, 200)
self.player1.addItems(mainWin.my_list)
self.player2 = ExtendedComboBox(self)
self.player2.resize(350,25)
self.player2.move(225, 275)
self.player2.addItems(mainWin.my_list)
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
运行代码时,出现错误“未定义名称'mainWin'”,并且我无法理解为什么它显示该错误。
该程序似乎是由“ self.player1.addItems(mainWin.my_list)”行和“ self.player2.addItems(mainWin.my_list)”行引起的,因为删除这些行时代码将显示窗口。
应该替换什么以便代码可以工作?
答案 0 :(得分:0)
您在声明mainWin之前先引用它。像这样:
self.player2.addItems(mainWin.my_list)
mainWin = MainWindow()
每次创建Select_player
类的实例时,它在初始化时都需要一个mainWin
的定义,但mainWin尚不存在,因此您需要移动定义在所有Select_player对象声明之前。
您应该可以通过将mainWin = MainWindow()
类定义上方的Select_player
行移动来解决此问题。或者,您可以仅将这行+ mainWin.Show()
添加到Select_player
类构造函数的开头