如何将相同的按键动作绑定到不同窗口中的按钮?

时间:2019-03-08 02:27:43

标签: python python-3.x tkinter frame key-bindings

我一直在搜索很多东西,但我仍然不知道是什么问题。我有两个窗口,要求一些值,结果执行了不同的功能。我试图将“ Enter”键绑定到不同窗口的按钮(因此应执行不同的功能),但这不起作用。

我唯一能做的就是将密钥绑定到整个Frame,然后将其取消绑定并再绑定一次。它可以工作,但是我很好奇为什么我不能将“ Enter”键链接到单独的按钮上而没有错误出现。提前致谢!

这是我的代码:

import tkinter as tk              

class Page(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.shared_data = {
            "number_of_frequencies": tk.StringVar(),
            "frequencies": []}

        container = tk.Frame(self)
        container.grid()
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def create_entry_widget(self, i):
        self.number = tk.StringVar()
        self.number.set('')
        self.shared_data["frequencies"].append(self.number)
        new_widget = tk.Entry(self.master, textvariable=self.number)
        return new_widget

    def show_frame(self, c):
        for frame in self.frames.values():
            frame.grid_remove()
        frame = self.frames[c]
        frame.grid()
        global number_of_frequencies
        try:
            number_of_frequencies = int(self.shared_data["number_of_frequencies"].get())
            for i in range(number_of_frequencies):
                tk.Label(self.master, text="Frequency "+str(i+1)).grid(row=i, column=0, padx=10, pady=5)
                self.entry_widgets = self.create_entry_widget(i)
                self.entry_widgets.grid(row=i, column=1, padx=10, pady=5)

            page1 = self.get_page(PageOne)

            tk.Button(self, text="Exit",
                      command=lambda: self.quitALL()).grid(row=i+1, column=0, padx=10, pady=10)

            self.button1 = tk.Button(self, text = "     Done     ", 
                      command=lambda: [page1.enter_freqs(), self.quitALL()])

            self.unbind_all('<Return>') # work
            self.bind('<Return>', (lambda a: [page1.enter_freqs(), self.quitALL()])) # work

#            self.button1.bind('<Return>', (lambda a: [page1.enter_freqs(), self.quitALL()])) #doesn't work

            self.button1.grid(row=i+1, column=1, padx=10, pady=10)

            frame.tkraise()
        except:
            frame.tkraise()

    def get_page(self, page_class):
        return self.frames[page_class]

    def quitALL(self):
        self.destroy()


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

        self.text = tk.Text(self, bg='gray93', bd=0, height=1, width=30, font=('Helvetica', 14))
        self.text.insert(tk.INSERT, "Please enter number of frequencies:")
        self.text.grid(padx=10, pady=10)

        entry = tk.Entry(self, textvariable=self.controller.shared_data["number_of_frequencies"])
        entry.grid(padx=10)

        self.controller.bind('<Return>', (lambda a: controller.show_frame(PageOne))) # work

        self.button = tk.Button(self, text="     Done     ",
                            command=lambda: controller.show_frame(PageOne))
#        self.button.bind('<Return>', (lambda a: controller.show_frame(PageOne))) # doesn't work

        self.button.grid(padx=10, pady=10) 


class PageOne(tk.Frame): 
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller=controller
        self.freq_list = self.controller.shared_data["frequencies"]
        self.freq_list_int = []

    def enter_freqs(self):
        for freq in self.freq_list:
            self.freq_list_int.append(int(freq.get()))
        self.func(self.freq_list_int)

    def func(self, freq_list):
        print('some stuff')


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

0 个答案:

没有答案