【问题标题】:Change number binding depending on class within Tkinter Frame根据 Tkinter Frame 中的类更改数字绑定
【发布时间】:2015-06-04 20:06:43
【问题描述】:

我有一个 Tkinter GUI,它由多个类组成,所有类都包含在一个框架中。所有的类都具有相同的维度,并且都同时加载在另一个之上。但是,用户输入决定了哪个类是可见的。每个类都具有与其他类相同的布局和按钮数量。即:3x3 网格的按钮。

我已经开始实现.bind() 函数,这些函数将每个按钮与数字键盘上的相应键相关联。问题在于,.bind() 函数在所有类中都因为 one 框架而保持静态。

如何将.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() 但是 self.bind 并没有改变任何东西。无论页面焦点如何,MainApp() 类中的初始绑定都会被执行。

【问题讨论】:

    标签: python user-interface tkinter key-bindings


    【解决方案1】:

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

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

    【讨论】:

    • 我尝试在每个班级中使用self.bind() 各种不同的方式,但没有运气。即:我尝试过的一个示例是为每个按钮定义一个功能。这包含网格(),按钮()等......然后通过.bind()调用该按钮功能(或执行的命令)但是,无论我尝试什么,初始绑定都是被执行的。另外,你能给我一个在哪里/如何使用 handlekey() 函数的例子吗?我无法通过搜索找到有用的 python 参考,也无法弄清楚如何使用它们。
    • @bitinerary: handlekey 只是我编的一个名字。你可以随心所欲地称呼它。大致思路是,绑定调用一个通用函数,通用函数将调用转发到特定页面。
    猜你喜欢
    • 2020-10-02
    • 2015-02-08
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多