Python 根据Tkinter帧中的类更改编号绑定
我有一个Tkinter GUI,它由几个类组成,所有这些类都包含在一个框架内。所有类都具有相同的维度,并且都同时加载到彼此的顶部。但是,用户输入决定了哪个类是可见的。每个类与其他类具有相同的布局和按钮数。ie:3x3按钮网格 我已经开始实现Python 根据Tkinter帧中的类更改编号绑定,python,user-interface,tkinter,key-bindings,Python,User Interface,Tkinter,Key Bindings,我有一个Tkinter GUI,它由几个类组成,所有这些类都包含在一个框架内。所有类都具有相同的维度,并且都同时加载到彼此的顶部。但是,用户输入决定了哪个类是可见的。每个类与其他类具有相同的布局和按钮数。ie:3x3按钮网格 我已经开始实现.bind()函数,这些函数将每个按钮与数字键盘上的相应键相关联。问题是由于一个框架,所有类中的.bind()函数都保持静态 如何将.bind()函数添加到以下脚本中,以根据当前可见的类调整?有效地更改命令,以便与相应的3x3按钮网格匹配 import sub
.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()
但是self.bind没有改变任何东西。MainApp()
类中的初始绑定是执行的绑定,与页面焦点无关。如果执行self.bind
而不是controller.bind
,则应运行特定于页面的函数,因为只有具有焦点的页面才会看到单击。这通常是处理绑定的方式——只绑定到绑定应该应用到的小部件。在这种情况下,您不需要全局绑定,而是需要每个页面的绑定
另一个解决方案是将它们全部绑定到
controller.handleKey(1)
,controller.handleKey(1)
等。handleKey
是一个通用函数,其唯一功能是找出当前页面,并调用当前帧的handleKey
。我已经尝试使用self.bind()
每门课都有各种不同的方式,没有运气。ie:我试过的一个例子是为每个按钮定义一个函数。这包含网格()、按钮()等。。。然后通过.bind()
调用该按钮函数(或执行的命令)。然而,无论我尝试了什么,初始绑定都是执行的绑定。另外,你能给我一个在哪里/如何使用handlekey()函数的例子吗。通过搜索,我找不到有用的python引用,也不知道如何准确地使用它们。@b概述:handlekey
只是我编的一个名字。你可以随便叫它什么。一般的想法是,绑定调用公共函数,公共函数将调用转发到特定页面。