尝试从wxpython中的另一个类访问列表时遇到AttributeError

时间:2018-06-21 13:03:11

标签: python user-interface wxpython

我有以下代码:

import wx
import pandas as pd
import pickle
from runpy import run_path

########################################################################
class Prototype(wx.Frame):

 #----------------------------------------------------------------------
 def __init__(self, parent, title):
      wx.Frame.__init__(self, None, title="Tennis Form", size=(800,600))
      self.UI()
      self.Centre()
      self.Show()

 #----------------------------------------------------------------------
 def UI(self):
      _icon = wx.NullIcon
      _icon.CopyFromBitmap(wx.Bitmap("C:\\Users\\XXX\\Desktop\\Project-Tennis\\Tennis-icon.png", wx.BITMAP_TYPE_ANY))
      self.SetIcon(_icon)
      self.SetBackgroundColour(wx.Colour(192, 192, 192))
      self.button_1 = wx.Button(self, wx.ID_ANY, "ATP")
      self.button_1.SetMinSize((250, 50))
      self.button_1.SetBackgroundColour(wx.Colour(216, 216, 191))
      self.button_2 = wx.Button(self, wx.ID_ANY, "WTA")
      self.button_2.SetMinSize((250, 50))
      self.button_2.SetBackgroundColour(wx.Colour(216, 216, 191))
      self.button_2.SetForegroundColour(wx.Colour(0, 0, 0))
      self.button_3 = wx.Button(self, wx.ID_ANY, "Update ATP")
      self.button_3.SetBackgroundColour(wx.Colour(216, 216, 191))
      self.button_3.SetForegroundColour(wx.Colour(0, 0, 0))
      self.button_4 = wx.Button(self, wx.ID_ANY, "Update WTA")
      self.button_4.SetBackgroundColour(wx.Colour(216, 216, 191))
      self.button_4.SetForegroundColour(wx.Colour(0, 0, 0))
      sizer_1 = wx.BoxSizer(wx.VERTICAL)
      sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
      sizer_1.Add((20, 200), 0, 0, 0)
      sizer_1.Add(self.button_1, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.LEFT | wx.RIGHT, 163)
      sizer_1.Add((20, 20), 0, 0, 0)
      sizer_1.Add(self.button_2, 0, wx.ALIGN_CENTER | wx.EXPAND | wx.LEFT | wx.RIGHT, 163)
      sizer_1.Add((20, 180), 0, 0, 0)
      sizer_2.Add((550, 20), 0, 0, 0)
      sizer_2.Add(self.button_3, 0, 0, 0)
      sizer_2.Add(self.button_4, 0, 0, 0)
      sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
      self.SetSizer(sizer_1)
      self.Layout()
      self.Centre()
      self.button_1.Bind(wx.EVT_BUTTON, self.OnButton1)
      self.button_2.Bind(wx.EVT_BUTTON, self.OnButton2)
      self.button_3.Bind(wx.EVT_BUTTON, self.OnButton3)
      self.button_4.Bind(wx.EVT_BUTTON, self.OnButton4)


 #----------------------------------------------------------------------
 def OnButton1(self, event):
      df = pd.read_pickle('C:/Users/XXX/Desktop/Project-Tennis/ATP_Rankings.pickle')
      self.my_list = df["Name"].tolist()
      frame = SecondFrame()
      frame.Show()

 def OnButton2(self, event):
      df = pd.read_pickle('C:/Users/XXX/Desktop/Project-Tennis/WTA_Rankings.pickle')
      self.my_list = df["Name"].tolist()
      frame = SecondFrame()
      frame.Show()

 def OnButton3(self, event):
      settings = run_path("C:/Users/XXX/Desktop/Project-Tennis/ATP-rankings-BS4.py")

 def OnButton4(self, event):
      settings = run_path("C:/Users/XXX/Desktop/Project-Tennis/WTA-rankings-BS4.py")

 ########################################################################
 class SecondFrame(wx.Frame):
 """"""

 #----------------------------------------------------------------------
 def __init__(self):
      """Constructor"""
      wx.Frame.__init__(self, None, title="Select Player", size=(800,600))
      panel = wx.Panel(self)
      self.Centre()
      self.UI()

 def UI(self):
      _icon = wx.NullIcon
      _icon.CopyFromBitmap(wx.Bitmap("C:\\Users\\XXX\\Desktop\\Project-Tennis\\Tennis-icon.png", wx.BITMAP_TYPE_ANY))
      self.SetIcon(_icon)
      self.SetBackgroundColour(wx.Colour(192, 192, 192))
      list1 = Prototype.my_list
      self.combo_box_1 = wx.ComboBox(self, wx.ID_ANY, choices = list1  , style=wx.CB_DROPDOWN | wx.CB_SORT)
      self.combo_box_2 = wx.ComboBox(self, wx.ID_ANY, choices=[], style=wx.CB_DROPDOWN | wx.CB_SORT)
      self.Run = wx.Button(self, wx.ID_ANY, "Run")
      self.Run.SetMinSize((150, 35))
      self.Run.SetBackgroundColour(wx.Colour(216, 216, 191))
      self.Run.SetForegroundColour(wx.Colour(0, 0, 0))
      self.combo_box_1.SetMinSize((450, 50))
      self.combo_box_2.SetMinSize((450, 50))
      sizer_1 = wx.BoxSizer(wx.VERTICAL)
      sizer_1.Add((20, 200), 0, 0, 0)
      sizer_1.Add(self.combo_box_1, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 0)
      sizer_1.Add((20, 20), 0, 0, 0)
      sizer_1.Add(self.combo_box_2, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 0)
      sizer_1.Add((20, 25), 0, 0, 0)
      sizer_1.Add(self.Run, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 0)
      self.SetSizer(sizer_1)
      self.Layout()
      self.Centre()


app = wx.App(False)
Prototype(None, title='')
app.MainLoop()

我尝试了wxpython和PyQt5,但最终都停留在同一点。

我想使用在主窗口(原型)上按下两个按钮之一时创建的列表,将其放入我在第二帧上创建的组合框中。

但是,我总是得到AttributeError:类型对象'Prototype'没有属性'my_list'。

按下一个按钮并在第二帧SecondFrame中使用列表时,如何访问在主窗口原型中创建的“ my_list”?

1 个答案:

答案 0 :(得分:1)

.my_list是实例属性,而不是类属性。您必须将列表传递给SecondFrame类构造函数:

class SecondFrame(wx.Frame):
    def __init__(self, the_list):
        wx.Frame.__init__(self, None, title="Select Player", size=(800,600)) 
        self.my_list = the_list
        # ...


    def UI(self):
        # ...
        self.combo_box_1 = wx.ComboBox(self, wx.ID_ANY, choices=self.my_list, style=wx.CB_DROPDOWN | wx.CB_SORT)


class Prototype(wx.Frame):
    # ....
    def OnButton1(self, event):
      df = pd.read_pickle('C:/Users/XXX/Desktop/Project-Tennis/ATP_Rankings.pickle')
      self.my_list = df["Name"].tolist()
      frame = SecondFrame(self.my_list)
      frame.Show()