【问题标题】:object has no attribute error in gui对象在gui中没有属性错误
【发布时间】:2023-03-15 14:17:02
【问题描述】:

我收到错误,因为“r1”对象没有属性“Frame_Display”。

class gui:

def __init__(self,master):
    Frame_Menu = LabelFrame(root, bd=1,text='Menu', labelanchor=N, height=550, width=200, relief=SUNKEN)
    Frame_Menu.grid(row=0, column=0)
    Frame_Display = LabelFrame(root, bd=1, text='System Layout', labelanchor=N, height=550, width=750, relief=SUNKEN)
    Frame_Display.grid(row=0, column=1, columnspan=3, rowspan=1, sticky=W+E+N+S)

class r1(gui):

def __init__(self, level=None, *args, **kwargs):
    super(gui, self).__init__(*args, **kwargs)
    vf1r1_button=tk.Button(self.Frame_Display,text="VF1R1",bg="red",state=DISABLED).grid(row=8,column=2,pady=(1,0))
    vf2r1_button=tk.Button(self.Frame_Display,text="VF2R1",bg="red",state=DISABLED).grid(row=10,column=2)
    r1_Button=tk.Button(self.Frame_Display,text="Reactor 1",bg="red").grid(row=9,column=4,padx=(30,0))

root=tk.Toplevel()
root.title("GUI Using classes")
a = r1()
root.mainloop()  

****edit 1 - 在下面添加回溯错误****

runfile('C:/Users/rohit/Desktop/GUI-Classes.py', wdir='C:/Users/rohit/Desktop')
Traceback (most recent call last):

  File "<ipython-input-20-4b3be26cf4d4>", line 1, in <module>
runfile('C:/Users/rohit/Desktop/GUI-Classes.py', wdir='C:/Users/rohit/Desktop')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/rohit/Desktop/GUI-Classes.py", line 62, in <module>
a = r1()

  File "C:/Users/rohit/Desktop/GUI-Classes.py", line 50, in __init__
vf1r1_button=tk.Button(self.Frame_Display,text="VF1R1",bg="red",state=DISABLED).grid(row=8,column=2,pady=(1,0))

AttributeError: 'r1' object has no attribute 'Frame_Display'  

我想将按钮放在主 gui 上的 r1 和 frame_display 中

【问题讨论】:

  • 请显示完整的回溯错误
  • @InAFlash 我已添加请检查
  • gui 类中,您应该将 Frame_Display 替换为 self.Frame_Display 以便将其定义为属性。
  • @LaurentH。也试过了,同样的错误
  • @Rohit HR,出现同样的错误,因为您的 super 调用无效。右边是super(r1, self).__init__(*args, **kwargs)。将此与@Laurent H. 评论和a = r1(master=root) 声明结合起来。

标签: python user-interface tkinter


【解决方案1】:

您的原始代码至少有 2 个问题可以解释错误:

  1. Frame_Display 未定义为类gui 中的属性:您必须使用self.Frame_Display 而不是Frame_Display

  2. 你错误地从r1类调用超类gui的__init__()函数:要这样做,你必须写gui.__init__(self, ...)

最后,这里有一个(简化的)代码,可以在我这边工作,还解决了其他小问题:

import tkinter as tk

class gui:

    def __init__(self, master):
        Frame_Menu = tk.LabelFrame(root, bd=1,text='Menu', height=550, width=200)
        Frame_Menu.grid(row=0, column=0)
        self.Frame_Display = tk.LabelFrame(root, bd=1, text='System Layout', height=550, width=750)
        self.Frame_Display.grid(row=0, column=1, columnspan=3, rowspan=1)

class r1(gui):

    def __init__(self, *args, **kwargs):
        gui.__init__(self, *args, **kwargs)
        vf1r1_button=tk.Button(self.Frame_Display,text="VF1R1",bg="red").grid(row=8,column=2,pady=(1,0))
        vf2r1_button=tk.Button(self.Frame_Display,text="VF2R1",bg="red").grid(row=10,column=2)
        r1_Button=tk.Button(self.Frame_Display,text="Reactor 1",bg="red").grid(row=9,column=4,padx=(30,0))


root=tk.Toplevel()
root.title("GUI Using classes")
a = r1(root)

root.mainloop() 

【讨论】:

  • 好答案。唯一的缺陷是您定义了def __init__(self, master):,然后在启动小部件时使用root,在这种情况下实际上是全局root
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2014-05-14
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多