' NoneType'对象没有属性' children'

时间:2015-10-23 13:51:34

标签: python tkinter

我这里有很多问题。当我运行代码时,它会创建一个窗口。但是当我试图关闭它时,我会得到"' NoneType'对象没有属性' children'"。此外,当在框中书写文本时,文本出现在所有三个框中。我做错了什么?

from Tkinter import *

class Window(Frame):
    def __init__(self, master=None):
        self.root = Tk()
        Frame.__init__(self, master)
        self.master = master
        self.init_window()
        self.root.geometry("400x300")
        self.root.mainloop()

    def init_window(self):
        self.root.title("GUI")
        self.ins = StringVar
        self.dele = StringVar
        self.upd = StringVar
        self.b1 = Button(self.root, text='insert(name,info,date)')
        self.b1.grid(row=0,column=0)
        self.b2 = Button(self.root, text='delete')
        self.b2.grid(row=1, column=0)
        self.b3 = Button(self.root, text='update')
        self.b3.grid(row=2, column=0)
        self.e1 = Entry(self.root, textvariable=self.ins)
        self.e1.grid(row=0, column=1)
        self.e2 = Entry(self.root, textvariable=self.dele)
        self.e2.grid(row=1, column=1)
        self.e3 = Entry(self.root, textvariable=self.upd)
        self.e3.grid(row=2, column=1)
        a = self.e1.get()
        b = self.e2.get()
        c = self.e3.get()

Window()

Here is the screenshot

2 个答案:

答案 0 :(得分:0)

由于这个Window()def __init__(self, master=None):你没有传入一个值,所以master默认为None。你应该在底部。

root = Tk()
Window(root)
root.mainloop()

删除self.root = Tk()

完整守则澄清:

from Tkinter import *

class Window(Frame):
    def __init__(self, master=None):
        self.master = master
        Frame.__init__(self, self.master)
        self.init_window()
        self.master.geometry("400x300")
        self.master.title("GUI")

    def init_window(self):
        self.ins = StringVar
        self.dele = StringVar
        self.upd = StringVar
        self.b1 = Button(self.master, text='insert(name,info,date)')
        self.b1.grid(row=0,column=0)
        self.b2 = Button(self.master, text='delete')
        self.b2.grid(row=1, column=0)
        self.b3 = Button(self.master, text='update')
        self.b3.grid(row=2, column=0)
        self.e1 = Entry(self.master, textvariable=self.ins)
        self.e1.grid(row=0, column=1)
        self.e2 = Entry(self.master, textvariable=self.dele)
        self.e2.grid(row=1, column=1)
        self.e3 = Entry(self.master, textvariable=self.upd)
        self.e3.grid(row=2, column=1)
        a = self.e1.get()
        b = self.e2.get()
        c = self.e3.get()

root = Tk()
Window(root)
root.mainloop()

答案 1 :(得分:0)

你的代码已经清理好了,但它必须根据你的目的进行修改。

from Tkinter import *

class Window():
    def __init__(self):
        self.root = Tk()
        self.init_window()
        self.root.geometry("400x300")
        self.root.mainloop()

    def get_values(self):
        print self.e1.get()
        print self.e2.get()
        print self.e3.get()

    def init_window(self):
        self.root.title("GUI")
        self.b1 = Label(self.root, text='insert(name,info,date)')
        self.b1.grid(row=0,column=0)
##        self.b2 = Button(self.root, text='delete')
##        self.b2.grid(row=1, column=0)
##        self.b3 = Button(self.root, text='update')
##        self.b3.grid(row=2, column=0)
        Button(self.root, text="print entry",
               command=self.get_values).grid(row=3, column=0)
        self.e1 = Entry(self.root)
        self.e1.grid(row=0, column=1)
        self.e2 = Entry(self.root, text="info")
        self.e2.grid(row=1, column=1)
        self.e3 = Entry(self.root, text="date")
        self.e3.grid(row=2, column=1)

Window()