简体   繁体   English

使用在PyQt5的另一个类中创建的列表

[英]Use a list created in another class in PyQt5

I have the following code: 我有以下代码:

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')
    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')
    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.player2 = ExtendedComboBox(self)
    self.player2.resize(350,25)
    self.player2.move(225, 275)

if __name__ == '__main__':

app = QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())

I created a main window (MainWindow) on which there are two buttons which both create a list as "my_list" when pushed. 我创建了一个主窗口(MainWindow),在该窗口上有两个按钮,当按下按钮时,这两个按钮都将列表创建为“ my_list”。 The list values are different depending on the button pushed. 列表值根据所按下的按钮而不同。

In the next window (Select_player) that opens when one of the button is pushed, there are two Combo boxes. 在按下一个按钮时打开的下一个窗口(Select_player)中,有两个组合框。

What I want to achieve is to have the values in "my_list" which were previously created in the main window into the two combo boxes. 我要实现的是将先前在主窗口中创建的“ my_list”中的值放入两个组合框。

I have tried something like self.player1.addItems(my_list) but I get the error "name 'my_list' is not defined". 我已经尝试过类似self.player1.addItems(my_list)的操作,但出现错误“名称'my_list'未定义”。

How can I use "my_list" created in the class MainWindow in Select_player? 如何在Select_player的MainWindow类中使用创建的“ my_list”?

my_list has local scope in the functions in which you defined it (clickMethod1() and clickMethod2()). my_list在定义它的函数中具有局部作用域(clickMethod1()和clickMethod2())。 my_list is not visible outside those functions. 在这些功能之外,my_list不可见。

You could declare my_list as an attribute of the MainWindow class - then you can access it as mainWin.my_list. 您可以将my_list声明为MainWindow类的属性-然后可以将其作为mainWin.my_list进行访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM