【问题标题】:Cursor event handling in python+Tkinterpython+Tkinter中的光标事件处理
【发布时间】:2010-09-17 15:41:10
【问题描述】:

我正在构建一个代码,当用户将光标的焦点从条目小部件更改为任何位置时,我希望能够在其中生成一个事件,例如另一个条目小部件、一个按钮...

到目前为止,我只提出了绑定到 TAB 和鼠标单击的想法,尽管如果我将鼠标单击绑定到 Entry 小部件,我只会在 Entry 小部件内获得鼠标事件。

如何在小部件失去光标焦点​​时生成事件?

提前致谢!

【问题讨论】:

    标签: python tkinter events mouse-cursor


    【解决方案1】:

    事件 是你想要的。运行以下示例,当焦点位于其中一个条目小部件中时,无论您单击还是按 Tab(或 shift-tab),您都会看到焦点输入和输出绑定。

    from Tkinter import *
    
    def main():
        global text
    
        root=Tk()
    
        l1=Label(root,text="Field 1:")
        l2=Label(root,text="Field 2:")
        t1=Text(root,height=4,width=40)
        e1=Entry(root)
        e2=Entry(root)
        l1.grid(row=0,column=0,sticky="e")
        e1.grid(row=0,column=1,sticky="ew")
        l2.grid(row=1,column=0,sticky="e")
        e2.grid(row=1,column=1,sticky="ew")
        t1.grid(row=2,column=0,columnspan=2,sticky="nw")
    
        root.grid_columnconfigure(1,weight=1)
        root.grid_rowconfigure(2,weight=1)
    
        root.bind_class("Entry","<FocusOut>",focusOutHandler)
        root.bind_class("Entry","<FocusIn>",focusInHandler)
    
        text = t1
        root.mainloop()
    
    def focusInHandler(event):
        text.insert("end","FocusIn %s\n" % event.widget)
        text.see("end")
    
    def focusOutHandler(event):
        text.insert("end","FocusOut %s\n" % event.widget)
        text.see("end")
    
    
    if __name__ == "__main__":
        main();
    

    【讨论】:

      【解决方案2】:

      这不是 tkinter 特有的,也不是基于焦点的,但我在这里得到了类似问题的答案:

      Detecting Mouse clicks in windows using python

      我有一段时间没有做任何 tkinter,但似乎有“FocusIn”和“FocusOut”事件。您也许可以绑定和跟踪这些以解决您的问题。

      来自: http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm

      【讨论】:

        猜你喜欢
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2013-11-08
        • 2018-06-23
        • 1970-01-01
        • 1970-01-01
        • 2017-06-12
        • 1970-01-01
        相关资源
        最近更新 更多