在用户定义的类中调用init项目时,我会收到一个属性

发布于 2025-02-08 22:26:08 字数 1719 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文