根据Tkinter Frame中的类更改数字绑定

时间:2015-06-04 20:06:43

标签: python user-interface tkinter key-bindings

我有一个Tkinter GUI,它由几个包含在一个帧中的类组成。所有类都具有相同的尺寸,并且彼此同时加载。但是,用户输入确定哪个类是可见的。每个类具有与其他类相同的布局和按钮数。即:3x3网格按钮。

我开始实施.bind()个功能,将每个按钮与数字键盘上的相应键相关联。问题是由于一个帧,.bind()函数在所有类中保持静态。

如何将.bind()个函数添加到以下脚本中,以便在当前可见的类上调整取决于?有效地更改命令以匹配相应的3x3按钮网格。

import subprocess
import Tkinter as tk

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

        container = tk.Frame(self)
        container.grid(column=5, row=15)
        container.grid_rowconfigure(0)
        container.grid_columnconfigure(0)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nswe")

        self.show_frame(StartPage)

        self.bind('0',(lambda event: self.show_frame(StartPage)))
        self.bind('1',(lambda event: self.show_frame(PageOne)))
        self.bind('2',(lambda event: self.show_frame(PageTwo)))

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

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

        def randomfunction0():
            subprocess.Popen('foobar', shell=True)

        def randomfunction1():
            subprocess.Popen('foobar', shell=True)

        StartPageButton0 = tk.Button(self, compound="top", image=self.StartImage0, text="foobar0", fg="black", command=lambda: subprocess.Popen('foobar0', shell=True))
        StartPageButton1 = tk.Button(self, compound="top", image=self.StartImage1, text="foobar1", fg="black", command=lambda: subprocess.Popen('foobar1', shell=True))
        StartPageButton2 = tk.Button(self, compound="top", image=self.StartImage2, text="foobar2", fg="black", command=lambda: subprocess.Popen('foobar2', shell=True))

        StartPageButton0.grid(row=1, column=1, padx=20, pady=10)
        StartPageButton1.grid(row=1, column=2, padx=20, pady=10)
        StartPageButton2.grid(row=1, column=3, padx=20, pady=10)

        controller.bind('1',(lambda event: subprocess.Popen('foobar0', shell=True)))
        controller.bind('2',(lambda event: subprocess.Popen('foobar1', shell=True)))
        controller.bind('3',(lambda event: subprocess.Popen('foobar2', shell=True)))

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

        Page1Button0 = tk.Button(self, compound="top", image=self.Page1Image0, text="foobar0", fg="black", command=lambda: subprocess.Popen('foobar0', shell=True))
        Page1Button1 = tk.Button(self, compound="top", image=self.Page1Image1, text="foobar1", fg="black", command=lambda: subprocess.Popen('foobar1', shell=True))
        Page1Button2 = tk.Button(self, compound="top", image=self.Page1Image2, text="foobar2", fg="black", command=lambda: subprocess.Popen('foobar2', shell=True))

        Page1Button0.grid(row=1, column=1, padx=20, pady=10)
        Page1Button1.grid(row=1, column=2, padx=20, pady=10)
        Page1Button2.grid(row=1, column=3, padx=20, pady=10)

        controller.bind('1',(lambda event: subprocess.Popen('foobar0', shell=True)))
        controller.bind('2',(lambda event: subprocess.Popen('foobar1', shell=True)))
        controller.bind('3',(lambda event: subprocess.Popen('foobar2', shell=True)))

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

        # PageOne Repeated with different subprocess.Popen commands.
if __name__ == "__main__":
    app = MainApp()
    app.title("foobar title")
    app.mainloop()

当我们在这里时,一个Frame设置也不允许我更改GUI窗口顶部显示的标题。我能在每节课中更改app.title("foobar title")吗?反对在所有课程中只显示一个标题。

编辑:我已尝试使用controller.bind()self.bind()但是self.bind并未改变任何内容。无论页面焦点如何,MainApp()类中的初始绑定都会被执行。

1 个答案:

答案 0 :(得分:2)

如果您执行self.bind而不是controller.bind,则应运行特定于页面的功能,因为只有具有焦点的页面才会看到该点击。这通常是您处理绑定的方式 - 仅绑定绑定应该应用于的小部件。在这种情况下,您不需要全局绑定,您需要为每个页面绑定。

另一个解决方案是将它们全部绑定到controller.handleKey(1)controller.handleKey(1)等。handleKey是一个通用函数,其唯一的功能是确定哪个页面是最新的,并调用当前帧的handleKey

相关问题