Python(Tkinter):无法使用.get()

时间:2017-03-22 09:58:23

标签: python class methods tkinter

我正在使用tkinter为我用Python2.7编写的程序创建UI。我修改了我在此处找到的代码:Switch between two frames in tkinter

我希望代码可以工作的方式是当"应用"按下按钮,将调用方法varset1varset2(取决于当前页面),这应该使用.get()命令将用户输入保存到变量。

但是,当我尝试将用户输入的条目(Initial,LastName,HouseNo和PostCode)保存到变量时,会返回此错误消息:

  

AttributeError:' NoneType'对象没有属性' get'

这是我的代码的简化版本,它仍然有点长,虽然这是我可以做到的简洁,以便重现错误:

import Tkinter as tk

class Setup(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (page1, page2):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("page1")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()

class page1(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        tk.Label(self, text="First Initial: ").grid(row=2, sticky=tk.E)
        self.Initial = tk.Entry(self).grid(row=2, column=1)
        tk.Label(self, text="Last Name: ").grid(row=3, sticky=tk.E)
        self.LastName = tk.Entry(self).grid(row=3, column=1)

        Quit = tk.Button(self, text="Exit",
                         command= self.quit).grid(row=13, column=0, sticky="sw")
        Next = tk.Button(self, text="OK",
                         command=lambda: controller.show_frame("page2")).grid(
                         row=13, column=1, sticky=tk.SE)
        Apply = tk.Button(self, text="Apply",
                          command = self.varset1).grid(row=13, column=1,
                          sticky=tk.SW)

    def varset1(self):
        initial = self.Initial.get()
        lastname = self.LastName.get()

class page2(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        tk.Label(self, text="House Number: ").grid(row=2, sticky=tk.E)
        self.HouseNo = tk.Entry(self).grid(row=2, column=1)
        tk.Label(self, text="Post Code: ").grid(row=3, sticky=tk.E)
        self.PostCode = tk.Entry(self).grid(row=3, column=1)

        Quit = tk.Button(self, text="Exit",
                         command= self.quit).grid(row=13, column=0, sticky="sw")
        Back = tk.Button(self, text="Back",
                         command=lambda: controller.show_frame("page1")).grid(
                         row=13, column=1, sticky=tk.SE)
        Apply = tk.Button(self, text="Apply",
                          command = self.varset2).grid(row=13, column=1,
                          sticky=tk.SW)

    def varset2(self):
        houseno = self.HouseNo.get()
        postcode = self.PostCode.get()

if __name__ == "__main__":
    app = Setup()
    app.mainloop()

我真的很感激任何帮助!

1 个答案:

答案 0 :(得分:2)

您为分配EntryButton所做的每个变量都使用以下格式进行格式化:

self.Initial = tk.Entry(self).grid(row=2, column=1)

这是错误的,因为tk.Entry(self)返回Entry对象,但tk.Entry(self).grid(row=2, column=1)返回None。所以self.Initial实际上是无。您需要做的是将您要从中检索数据的每个EntryButton分成两行:

self.Initial = tk.Entry(self)
self.Initial.grid(row=2, column=1)

你应该改变每一个人的纵容,并避免进一步的类似错误(这是你应该首先做的)......

self.LastName = tk.Entry(self).grid(row=3, column=1)
# into:
self.LastName = tk.Entry(self)
self.LastName.grid(row=3, column=1)
# and so on....