首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >斜纹尿布之间的通信

斜纹尿布之间的通信
EN

Stack Overflow用户
提问于 2020-04-07 20:44:15
回答 2查看 206关注 0票数 0

我正在做一个小项目,制作了一个屏幕上的小键盘,作为一个平移器。

我的应用程序是这样构建的:

(Entry-Widget)

  • input 2 (Entry-Widget)

  • input 3 (Text-Widget)

  • on_screen-keyboard (Toplevel-Widget)

  • 根窗口(Tk-Widget)
    • 输入

Toplevel-Widget包含按钮,回调应该在条目中输入文本(就像键盘按钮一样)

我想要的是键盘/键盘的子级和最后一个活动输入-Widget之间的通信。我的问题是,我不知道怎么说键盘,它应该向哪个输入-Widget发送消息。

代码语言:javascript
复制
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()

在这里,主窗口的输入和键盘类的闪烁是很重要的.

代码已准备好调试。

我更喜欢一种解决方案,类似于因特网上的通信(sender=button、接收者id、消息),但非常欢迎每一种工作解决方案。

顺便说一句:我也在寻找一种解决方案,我不需要强迫输入聚焦,而Toplevel仍然是屏幕的最高层(如果我聚焦that/其中一个输入/按钮,键盘将保持在它前面)

摘要:如何找出三个输入小部件中的哪一个最终是活动的,而键盘-toplevel已经是焦点了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-08 14:29:31

您的代码可能有一个更好的结构。(但我没有修改您的代码结构。)

后面是您的代码,我使用了全局variable.And修复您的code.And中的一些错误--它可以在我的计算机中正常工作。

代码语言:javascript
复制
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()
        global focusedWidget
        focusedWidget = None
        self.text = tk.StringVar(value="")
        self.input1 = ttk.Entry(self)
        self.input1.bind("<FocusIn>", self.getFocusWidget)
        self.input2 = ttk.Entry(self)
        self.input2.bind("<FocusIn>", self.getFocusWidget)
        self.input3 = tk.Text(self, height=3, width=15)
        self.input3.bind("<FocusIn>", self.getFocusWidget)
        self.input1.pack()
        self.input3.pack()
        self.input2.pack()

    def getFocusWidget(self,event): # this function could be a static function
        global focusedWidget
        focusedWidget = event.widget

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=lambda button=button:self.bclicked(button)) # this lambda expression has some errors.
            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):
        global focusedWidget

        """
        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"])
        """
        if not focusedWidget: # If user hasn't click a entry or text widget.
            print("Please select a entry or text")
            return
        if focusedWidget.widgetName=='ttk::entry': # use if statement to check the type of selected entry.
            focusedWidget.insert(index=tk.INSERT,string=button["text"])
        else:
            focusedWidget.insert("end",button["text"])


    def close(self):
        Keyboard.OPENED = False
        self.destroy()
        print("keyboard closed!")


root = MainWindow()
root.mainloop()
票数 0
EN

Stack Overflow用户

发布于 2020-04-08 15:27:56

我可能做了比需要更多的更改,但主要集中在方法keyboard_triger()pass_key_to_master()上,这两个方法都使用变量master实现的思想,可以访问超出作用域的调用方法。方法set_focused_object()存储对最后一个对象的引用,注意比它存储小部件,而不是事件,它比每次搜索对象更容易

代码语言:javascript
复制
import tkinter as tk
from tkinter import ttk

class MainWindow(tk.Tk):

    def keyboard_triger(self, key):
        # to identify wath object is just use
        # isinstance(self.active_input, ttk.Entry)
        self.active_input.insert(tk.END, key)

    def new_keyboard(self):
        Keyboard(self)

    def set_focused_object(self, event):
        self.active_input = event.widget

    def __init__(self):
        tk.Tk.__init__(self)
        self.active_input = None

        ttk.Button(self, text="Show Keyboard", command=self.new_keyboard).pack()

        self.input1 = ttk.Entry(self)
        self.input1.bind("<FocusIn>", self.set_focused_object)
        self.input1.pack()

        self.input2 = ttk.Entry(self)
        self.input2.bind("<FocusIn>", self.set_focused_object)
        self.input2.pack()

        self.input3 = tk.Text(self, height=3, width=15)
        self.input3.bind("<FocusIn>", self.set_focused_object)
        self.input3.pack()

class Keyboard(tk.Toplevel):

    def pass_key_to_master(self, key):
        self.master.keyboard_triger(key)

    def __init__(self, master):
        tk.Toplevel.__init__(self, master)

        self.master = master
        self.title('Keyboard')

        # this way of agruping keys stores the kwags
        # of the drawing method
        keys = {
            'A': {'x': 0, 'y': 0},
            'B': {'x': 20, 'y': 20},
            'C': {'x': 50, 'y': 50}
        }

        # expected structure
        # {string key: reference to the button}
        self.buttons = {}
        for i in keys:
            self.buttons[i] = tk.Button(  # i=i is required to make a instance
                self, text=i, command=lambda i=i: self.pass_key_to_master(i)
            )
            self.buttons[i].place(**keys[i])


if __name__ == '__main__':
    root = MainWindow()
    root.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61089116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档