嗨,我正在做一个小项目,并在屏幕上制作了一个小面板,作为tkinter Toplevel的产品>
我的应用程序是这样构建的:
Toplevel-Widget包含Buttons,wichs回调应在条目中输入文本(就像键盘Buttons一样]
我想要的是键盘子/键盘和最后一个活动输入小部件
之间的通信。我的问题是,我不知道该怎么说键盘,将输入小部件夹在中间,它应该发送消息。import tkinter as tk from tkinter import ttk class MainWindow(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.active_input = tk.Variable(value=None) ttk.Button(self, text="show Keyboard", command=lambda: Keyboard(self)).pack() self.text = tk.StringVar(value="") self.input1 = ttk.Entry(self) self.input1.bind("<FocusIn>", lambda e: self.active_input.set(self.input1)) self.input2 = ttk.Entry(self) self.input2.bind("<FocusIn>", lambda e: self.active_input.set(self.input2)) self.input3 = tk.Text(self, height=3, width=15) self.input3.bind("<FocusIn>", lambda e: self.active_input.set(self.input3)) self.input1.pack() self.input3.pack() self.input2.pack() class Keyboard(tk.Toplevel): OPENED = False NAME = "- Keyboard -" NUM = [{"text":"1", "width":1}, {"text":"2", "width":1}, {"text":"3", "width":2}] CHAR= [{"text":"A", "width":1}, {"text":"B", "width":1}, {"text":"C", "width":2}] def __init__(self, master): if not Keyboard.OPENED: Keyboard.OPENED = True print("keyboard opened!") self.master = master tk.Toplevel.__init__(self, master) self.title(self.NAME) self.protocol("WM_DELETE_WINDOW", self.close) self.keyb_nb = ttk.Notebook(self) self.keyb_nb.pack() self.num_tab = ttk.Frame(self.keyb_nb) self.createPad(self.num_tab, Keyboard.NUM,2) self.keyb_nb.add(self.num_tab, text="123") self.char_tab = ttk.Frame(self.keyb_nb) self.createPad(self.char_tab, Keyboard.CHAR, 2) self.keyb_nb.add(self.char_tab, text="ABC") def createPad(self, master, pad:list, max_col): self.co_count = 0 self.ro = 1 for button in pad: button["id"] = ttk.Button(master, width=6*button["width"], text=button["text"], command=self.bclicked(button)) if self.co_count >= max_col: self.ro = self.ro + 1 self.co_count = 0 button["id"].grid(row=self.ro, columnspan=button["width"], column=self.co_count) self.co_count = self.co_count+button["width"] def bclicked(self, button:dict): """ reciver = self.master.active_input #I think the Problem here is, that the variable contains a string, not a widget reciever.focus_force() reciever.insert(index=tk.INSERT, string=button["text"]) """ pass def close(self): Keyboard.OPENED = False self.destroy() print("keyboard closed!") root = MainWindow() root.mainloop()
这里是Mainwindow的初始化,而Keyboard类的单击则很重要...
代码已调试就绪
我更喜欢一种解决方案,类似于Internet上的通信(发送者=按钮,接收方ID,消息),但非常欢迎每一个可行的解决方案
btw:我也在寻找一种解决方案,我不必强制输入集中,而顶层保持在屏幕的最高层(如果我集中Tk-Widget /输入之一/按钮,键盘将停留在它的前面)
感谢您的帮助;)
摘要:当键盘顶层已经成为焦点时,我怎么找出这三个输入小部件中最后一个是活动的?
嗨,我正在做一个小项目,并在tkinter上制作了一个屏幕上的Kexboard。小部件)...
您的代码可能有更好的结构。