根据Tkinter框架内的类更改数字绑定



我有一个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()

虽然我们在它,一个框架设置也不允许我改变标题显示在GUI窗口的顶部。我能在每个类中改变app.title("foobar title")吗?而不是在所有类中只显示一个标题。

编辑:我试过使用controller.bind()self.bind(),但自我。Bind不会改变任何东西。无论页面焦点如何,MainApp()类中的初始绑定都会被执行。

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

另一个解决方案是将它们全部绑定到controller.handleKey(1), controller.handleKey(1)等。handleKey是一个泛型函数,它的唯一功能是找出当前页面,并调用当前框架的handleKey

最新更新