【问题标题】:widget.unbind removes all callbacks for a sequence not just the funcid callbackwidget.unbind 删除序列的所有回调,而不仅仅是 funcid 回调
【发布时间】:2022-10-02 19:52:59
【问题描述】:

我希望unbind( seq, funcid = _id ) 仅从绑定到该小部件和序列的事件中删除_id 回调。相反,它会删除所有这些。我得到相同的行为 macOS 和 Linux。

我做错了什么或误解了文档吗?

import tkinter as tk

click1_id = None # The id for the added function. 

def add_binding():
    global click1_id
    print( \'Add Binding\', label.bind(), click1_id, end = \'  \' ) # Tracing
    if click1_id is None:
        click1_id = label.bind( \"<Button>\", on_click1(), add = \'+\' )
        # on_click1() returns a function with sequential numbering.
    print( \'Added Binding\', label.bind(), click1_id, \'\\n\' ) # Tracing

def remove_binding():
    global click1_id
    print( \'Remove\', click1_id, end = \"  \" ) # Tracing
    if click1_id:
        print( \'Unbinding\', click1_id ) # Tracing

        label.unbind( \"<Button>\", funcid = click1_id ) 
        # This removes all bindings, not just the funcid 

        click1_id = None
    print( \'Bindings:\', label.bind(), \'ID:\', click1_id, \'\\n\' ) # Tracing

def on_click0( event ):
    print( \"Click: 0\", event, \'Widget:\', event.widget )

def on_click_root( event ):
    # Detects clicks even if all events on the Label are unbound
    print( \"Click root\", event, \'Widget:\', event.widget, \'\\n\' )

seq = 0
def on_click1():
    \"\"\" Returns a function that reports with a sequence number. \"\"\" 
    global seq
    seq += 1
    count = seq

    def func( event ):
        print( \'Click:\', count, event, \'Widget:\', event.widget )
    return func

root = tk.Tk()

label = tk.Label( root, text = \'Click to action\' )

doadd = tk.Button( root, text = \' Add  binding \', command = add_binding )
doremove = tk.Button( root, text = \'Remove binding\', command = remove_binding )

label.bind( \"<Button>\", on_click0 )
root.bind( \"<Button>\", on_click_root )

label.grid( row = 0, column = 0, columnspan = 2, pady = 5 )
doadd.grid( row = 1, column = 0, padx = 5, pady = 5 )
doremove.grid( row = 1, column = 1, padx = 5, pady = 5 )

root.mainloop()

一系列动作后的追踪报告。

# Click Label - As expected
Click: 0 <ButtonPress event num=1 x=40 y=15> Widget: .!label        
Click root <ButtonPress event num=1 x=40 y=15> Widget: .!label 

# Click Add Binding - As expected
Click root <ButtonPress event num=1 x=56 y=12> Widget: .!button    

Add Binding (\'<Button>\',) None  Added Binding (\'<Button>\',) 140691558063232func 

# Click Label - As expected
Click: 0 <ButtonPress event num=1 x=39 y=14> Widget: .!label
Click: 1 <ButtonPress event num=1 x=39 y=14> Widget: .!label
Click root <ButtonPress event num=1 x=39 y=14> Widget: .!label 

# Click Remove Binding ???
Click root <ButtonPress event num=1 x=58 y=12> Widget: .!button2 

Remove 140691558063232func  Unbinding 140691558063232func
Bindings: () ID: None   # Expected Bindings: (\'<Button>\',) 

# Click Label - Click: 0 expected - only Click root reported
Click root <ButtonPress event num=1 x=62 y=11> Widget: .!label 

# Click Add Binding
Click root <ButtonPress event num=1 x=74 y=17> Widget: .!button 

Add Binding () None  Added Binding (\'<Button>\',) 140691567712256func 

# Click Label - Only Click 2 reported
Click: 2 <ButtonPress event num=1 x=48 y=15> Widget: .!label
Click root <ButtonPress event num=1 x=48 y=15> Widget: .!label 

    标签: tkinter


    【解决方案1】:

    unbind 方法不能以我理解文档的方式工作。 widget.unbind( sequence, funcid = _id) 将所有回调与序列解除绑定,而不仅仅是与funcid = _id 绑定的回调。添加 funcid 参数意味着对函数的 tcl/tk 引用被删除以及被解除绑定。

    an issue raised for tkinter in 2017 这基本上是同一个问题,一些讨论为我澄清了一些事情。

    【讨论】:

      猜你喜欢
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多