对象在gui中没有属性错误

时间:2018-06-20 09:56:30

标签: python user-interface tkinter

我收到错误消息,因为“ 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()  

****编辑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中

1 个答案:

答案 0 :(得分:0)

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

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

  2. 您错误地从类__init__()中调用了超类gui的r1函数:为此,您必须编写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()