麻烦在TKinter中绑定事件

时间:2013-02-28 13:09:15

标签: python user-interface event-handling tkinter

对于背景:我的大多数经验是ruby / rails。我正在尝试通过构建一个简单的GUI应用程序来帮助朋友,该应用程序更新Excel文件并且没有太多Python或TKinter的经验。目标是使用一个简单的表单,用户输入一个数字,另一个表单显示一个下拉菜单。我决定将给定的数字存储在一个全局变量中,因为我在尝试在两个帧之间传递变量时遇到了麻烦。我无法设置全局变量并切换到第二帧。我遇到的其他问题/问题是##标记的评论。

或者,如果有人对制作可以访问MDB或excel文件的跨平台应用程序的最佳方法有任何想法,我会全力以赴。这让我感到非常困难。谢谢你的帮助。

import Tkinter as tk

TITLE_FONT = ("Helvetica", 18, "bold")
ID_NUMBER = None

class StatusApp(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 (EntryPage, StatusPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(EntryPage)

    def show_frame(self, c):
        '''Show a frame for the given class'''
        frame = self.frames[c]
        frame.tkraise()

class EntryPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="Enter ID:", font=TITLE_FONT)
        self.entry = tk.Entry(self)
        ## Using the lambda works to switch frames, but I need to be able to execute 
        ## multiple statements.
        # entry.bind('<Return>', lambda event: controller.show_frame(StatusPage))

        ## In examples I've seen, callback has been used without the empty parens, not sure
        ## why they're needed?
        self.entry.bind('<Return>', self.callback())
        label.pack(side="top", fill="x", pady=10)
        self.entry.pack()
        self.entry.focus_set()

    def callback(self):
        ## I noticed the following gets fired once the program starts
        print 'hello'

        ## For some reason it says that entry doesn't have the attribute 'set'. I don't
        ## understand this as I'm calling it like a method.
        self.entry.set('hello')

        ## Ultimately setting the global ID_NUMBER variable is one of the main goals of this
        ## function
        ID_NUMBER = self.entry.get()

        ## I haven't been able to switch frames from within this function, only w/ a lambda as
        ## seen on line 34.
        # show_frame(StatusPage())

class StatusPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="ID: ", font=TITLE_FONT)
        optionList = ('train', 'plane', 'boat')
        selected_opt = tk.StringVar()
        selected_opt.set(ID_NUMBER)
        menu = tk.OptionMenu(self, selected_opt, *optionList)
        button = tk.Button(self, text="Save", command=lambda: controller.show_frame(EntryPage))
        label.pack(side="top", fill="x", pady=10)
        menu.pack()
        button.pack()

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

1 个答案:

答案 0 :(得分:0)

绑定的对象必须是对可调用函数的引用。 Lambda经常在此上下文中使用,因为它创建了一个匿名函数并返回一个引用。

执行...bind(..., self.callback())时,在bind语句执行时调用该函数。在代码中,您发表评论## I noticed the following gets fired once the program starts;这就是为什么。此函数调用的结果是与绑定相关联的内容。通常,在您的特定情况下,这是值None。您必须省略()

在您编写的代码评论中

## For some reason it says that entry doesn't have the attribute 'set'. I don't
## understand this as I'm calling it like a method.
self.entry.set('hello')

是什么让您相信条目小部件有set方法?我所知道的文件没有提出这种说法。错误消息是正确的,条目小部件没有名为“set”的属性(在此上下文中,函数被视为属性)。