在用户定义的类中调用init项目时,我会收到一个属性
class Schedule():
def __init__(self, master):
self.master = master
master.title('Schedule')
master.geometry('800x800')
self.Main = Frame(self.master)
self.Main.pack(fill = BOTH)
#Widgets
self.treeFrame = Frame(self.Main, height = 690, width = 800)
self.treeFrame.pack(side = BOTTOM, expand = False)
self.PGvariable = StringVar()
self.PGvariable.set(ProduktionsGruppe[0])
self.ProduktionsGruppeOptionMenu = OptionMenu(self.optionsFrame, self.PGvariable,
'Gruppe auswählen', *ProduktionsGruppe.values(), command = Schedule.selectPG)
self.ProduktionsGruppeOptionMenu.pack(pady = 10)
self.tree = Treeview(self.treeFrame, show = 'headings', height = 32)
self.selectPG()
def selectPG(self):
#delete existing tree
for i in self.tree.get_children():
self.tree.delete(i)
#get production group selection
PGselection = self.PGvariable.get()
global PGselectionint
self.PGvariable.set(ProduktionsGruppe[PGselectionint])
#create tree with list of selected group's names as columns
treeview_columns = listofnumberlists[PGselectionint]
self.tree['columns'] = treeview_columns
if __name__ == '__main__':
root = Tk()
Schedule(root)
root.mainloop()
这是代码的相关部分。它旨在显示一个可以通过用户从OptionMenu选择选项来更改的树视图。当我运行它时,它会恰好提起TreeView,但是当我从选项菜单中选择一个选项时,我会在此属性中出现
for i in self.tree.get_children():
AttributeError: 'str' object has no attribute 'tree'
相同的错误,在我尝试从 init 在类中的不同功能中函数。 有人知道如何解决这个问题吗?
class Schedule():
def __init__(self, master):
self.master = master
master.title('Schedule')
master.geometry('800x800')
self.Main = Frame(self.master)
self.Main.pack(fill = BOTH)
#Widgets
self.treeFrame = Frame(self.Main, height = 690, width = 800)
self.treeFrame.pack(side = BOTTOM, expand = False)
self.PGvariable = StringVar()
self.PGvariable.set(ProduktionsGruppe[0])
self.ProduktionsGruppeOptionMenu = OptionMenu(self.optionsFrame, self.PGvariable,
'Gruppe auswählen', *ProduktionsGruppe.values(), command = Schedule.selectPG)
self.ProduktionsGruppeOptionMenu.pack(pady = 10)
self.tree = Treeview(self.treeFrame, show = 'headings', height = 32)
self.selectPG()
def selectPG(self):
#delete existing tree
for i in self.tree.get_children():
self.tree.delete(i)
#get production group selection
PGselection = self.PGvariable.get()
global PGselectionint
self.PGvariable.set(ProduktionsGruppe[PGselectionint])
#create tree with list of selected group's names as columns
treeview_columns = listofnumberlists[PGselectionint]
self.tree['columns'] = treeview_columns
if __name__ == '__main__':
root = Tk()
Schedule(root)
root.mainloop()
This is the relevant portion of the code. It is meant to display a treeview that can be changed by the user selecting an option from the OptionMenu. When I run it, it pulls up the treeview fine, but when I select an option from the Option menu I get this AttributeError
for i in self.tree.get_children():
AttributeError: 'str' object has no attribute 'tree'
The same error comes up during every instance where I try to call a variable from the init function in a different function within the class.
Does anyone know how to resolve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论