【发布时间】: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