继承和访问实例变量的问题 Python
Problems with inheritance and accessing instance variables Python
我是 OOP 和 GUI 的新手。我正在使用 python 使用 tkinter 制作顶层。这个顶层由两个 classes 组成,其中一个继承了另一个的属性和实例函数。但是我无法读取和获取存储在 class 的 init 方法中的一些值。我应该怎么做?
我没有办法解决这个问题。
class布局(tk.Toplevel):
def __init__(self, title, labelName):
tk.Toplevel.__init__(self)
self.title(title)
...其他小部件....
self.Scrolly = tk.Scrollbar(self)
self.Scrollx = tk.Scrollbar(self)
self.Listbox = tk.Listbox(self)
self.Listbox.configure(selectmode=tk.MULTIPLE, yscrollcommand=self.Scrolly.set,
xscrollcommand=self.Scrollx.set)
self.Listbox.insert(tk.END, "ciao")
self.Scrolly.configure(command=self.Listbox.yview())
self.Scrollx.configure(command=self.Listbox.xview(), orient=tk.HORIZONTAL)
self.Scrolly.grid(row=1 , column=0, rowspan=5)
self.Scrollx.grid(row=6, column=1)
self.Listbox.grid(row=1, column=1, rowspan=5)
其他具有关联功能的小部件
def add(self):... #add elements to Listbox
def Remove(self, opt=None):...
class Mod_features(布局):
def __init__(self):
super().__init__("Aggiungi/rimuovi esercizi", "Esercizi:")
self.Populate_list()
def Populate_list(self):
print(super().Listbox.get(0, tk.END))
错误:
在 Populate_list
打印(超级()。Listbox.get(0,tk.END))
AttributeError: 'super' 对象没有属性 'Listbox'
您应该可以打电话给 self.Listbox.get(0, tk.END)
。
Mod_features
扩展了 Layout
,因此 Listbox
也应该是 Mod_features
的成员。
我是 OOP 和 GUI 的新手。我正在使用 python 使用 tkinter 制作顶层。这个顶层由两个 classes 组成,其中一个继承了另一个的属性和实例函数。但是我无法读取和获取存储在 class 的 init 方法中的一些值。我应该怎么做?
我没有办法解决这个问题。
class布局(tk.Toplevel):
def __init__(self, title, labelName):
tk.Toplevel.__init__(self)
self.title(title)
...其他小部件....
self.Scrolly = tk.Scrollbar(self)
self.Scrollx = tk.Scrollbar(self)
self.Listbox = tk.Listbox(self)
self.Listbox.configure(selectmode=tk.MULTIPLE, yscrollcommand=self.Scrolly.set,
xscrollcommand=self.Scrollx.set)
self.Listbox.insert(tk.END, "ciao")
self.Scrolly.configure(command=self.Listbox.yview())
self.Scrollx.configure(command=self.Listbox.xview(), orient=tk.HORIZONTAL)
self.Scrolly.grid(row=1 , column=0, rowspan=5)
self.Scrollx.grid(row=6, column=1)
self.Listbox.grid(row=1, column=1, rowspan=5)
其他具有关联功能的小部件
def add(self):... #add elements to Listbox
def Remove(self, opt=None):...
class Mod_features(布局):
def __init__(self):
super().__init__("Aggiungi/rimuovi esercizi", "Esercizi:")
self.Populate_list()
def Populate_list(self):
print(super().Listbox.get(0, tk.END))
错误: 在 Populate_list 打印(超级()。Listbox.get(0,tk.END)) AttributeError: 'super' 对象没有属性 'Listbox'
您应该可以打电话给 self.Listbox.get(0, tk.END)
。
Mod_features
扩展了 Layout
,因此 Listbox
也应该是 Mod_features
的成员。