找不到属性的grid_forget()错误

时间:2019-05-01 20:48:34

标签: python tkinter

我正在使用tkinter进行一个小项目,以使窗口的中间框架发生变化

    import tkinter as tk
    root = tk.Tk()


    def input_data():
      personal_data_frame.grid_forget()
      input_data_frame = ttk.LabelFrame(root, weight=0)
      input_data_frame.grid(row=1, column=0, sticky="NSWE", weight=1)

    def personal_data():
      personal_data_frame = ttk.Frame(root, weight=1)
      personal_data_frame.grid(row=1, column=0)
    personal_data()
    input_data()

因此,对于我来说,它似乎无法删除个人数据框架(为简单起见,我在框架中有一些小部件)

我得到的错误是“ AttributeError:对象没有属性grid_forget” 一般而言,我几乎完全是tkinter的新手,但我不打算放弃

1 个答案:

答案 0 :(得分:0)

您的代码有很多错误,并且没有给出错误AttributeError: object has no attribute grid_forget

我的示例在删除Frame时没有问题。它将Frame分配给全局变量,然后其他函数可以访问此Frame并可以将其删除。

import tkinter as tk
import tkinter.ttk as ttk

def input_data():
    personal_data_frame.grid_forget()
    input_data_frame = ttk.LabelFrame(root)#, weight=0) # ERROR
    input_data_frame.grid(row=1, column=0, sticky="NSWE")#, weight=1) # ERROR

def personal_data():
    global personal_data_frame

    personal_data_frame = ttk.Frame(root)#, weight=1) # ERROR
    personal_data_frame.grid(row=1, column=0)

root = tk.Tk()
personal_data()
input_data()
root.mainloop()

如果您给AttributeError: object has no attribute grid_forget分配了与tkinter的窗口部件不同的内容,即personal_data_frame,可能会遇到问题。字符串,object()

此代码显示错误AttributeError: object has no attribute grid_forget,因为我在object()之后将personal_data_frame分配给personal_data()并且object()没有方法grid_forget()

import tkinter as tk
import tkinter.ttk as ttk

def input_data():
    personal_data_frame.grid_forget()
    input_data_frame = ttk.LabelFrame(root)#, weight=0) # ERROR
    input_data_frame.grid(row=1, column=0, sticky="NSWE")#, weight=1) # ERROR

def personal_data():
    global personal_data_frame

    personal_data_frame = ttk.Frame(root)#, weight=1) # ERROR
    personal_data_frame.grid(row=1, column=0)


root = tk.Tk()
personal_data()
personal_data_frame = object()
input_data()
root.mainloop()