Python Tkinter只能打开unbind和rebind按钮1

2024-09-29 05:24:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试停用链接到父函数的按钮1。我需要重新分配按钮1来移动画布上的图像。所有这些在第一次执行代码时都能正常工作。第二次出现这个错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1540, in __call__
    return self.func(*args)
  File "./wman2", line 244, in move_window
    MoveWindow(mycanvas, _item_id, _canvas_xy, _b1_bind, popup)
  File "./wman2", line 256, in __init__
    self.canvas.unbind("<ButtonPress-1>", self.b1_bind)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1102, in unbind
    self.deletecommand(funcid)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 450, in deletecommand
    self.tk.deletecommand(name)
TclError: can't delete Tcl command

代码:

我已经注释掉了生成以下错误的代码:

def move_window ():
    global _b1_bind, popup
    ''' Can only move a window, not a monitor or entire canvas '''
    if _item_type == "Window" :
        MoveWindow(mycanvas, _item_id, _canvas_xy, _b1_bind, popup)

    else :
        display_info()

class MoveWindow:
    ''' Drag image on canvas '''

    def __init__(self, canvas, item, coords, b1_bind, func_bind):
        self._drag_data = {"x": 0, "y": 0, "item": None}
        self.item = item
        self.coords = coords
        self.canvas = canvas
        self.b1_bind = b1_bind
        self.func_bind = func_bind
#        self.canvas.unbind("<ButtonPress-1>", self.b1_bind)
        self.p_bind = self.canvas.bind("<ButtonPress-1>", \
                                       self.on_image_press, "+")
        self.r_bind = self.canvas.bind("<ButtonRelease-1>", \
                                       self.on_image_release, "+")
        self.m_bind = self.canvas.bind("<B1-Motion>", \
                                       self.on_image_motion, "+")

    def on_image_press(self, event):
        '''Begining drag of an object'''
        root.config(cursor='hand1')
        # record the item and its location
        self._drag_data["item"] = self.item
        self._drag_data["x"] = self.coords[0]
        self._drag_data["y"] = self.coords[1]
        ''' Initial call in case mouse off image '''

    def on_image_release(self, event):
        '''End drag of an object'''
        root.config(cursor='')
        self.canvas.unbind("<ButtonPress-1>", self.p_bind)
        self.canvas.unbind("<ButtonRelease-1>", self.r_bind)
        self.canvas.unbind("<B1-Motion>", self.m_bind)
#        self.b1_bind = self.canvas.bind("<Button-1>", self.func_bind, "+")

    def on_image_motion(self, event):
        '''Handle dragging of an object'''
        # compute how much the mouse has moved
        delta_x = event.x - self._drag_data["x"]
        delta_y = event.y - self._drag_data["y"]
        # move the object the appropriate amount
        self.canvas.move(self._drag_data["item"], delta_x, delta_y)
        # record the new position
        self._drag_data["x"] = event.x
        self._drag_data["y"] = event.y

再往下看代码:

# attach popup to canvas
menu.bind("<FocusOut>", popup_focus_out)
_b3_bind = mycanvas.bind("<Button-3>", popup, "+")
# Button 1 conflicts with MoveWindow()
_b1_bind=None
#_b1_bind = mycanvas.bind("<Button-1>", popup, "+")

摘要

我要说的三句话是:

_b1_bind = mycanvas.bind("<Button-1>", popup, "+")
self.canvas.unbind("<ButtonPress-1>", self.b1_bind)
self.b1_bind = self.canvas.bind("<Button-1>", self.func_bind, "+")

目前我唯一的选择是不让父函数使用按钮1。我认为问题是第一次将button 1重新分配给父级时,button ID发生了变化,需要将其重新分配给全局变量_b1_bind,但我不确定如何做到这一点

信用:Code来自Brian Oakley


Tags: inimageselfeventdatabindonlib
1条回答
网友
1楼 · 发布于 2024-09-29 05:24:04

多亏了stovfl的帮助,它现在开始工作了

解决方案是将_b1_bind从全局变量更改为封装在mycanvas中的变量

第一步是更改此行:

_b1_bind = mycanvas.bind("<Button-1>", popup, "+")

收件人:

mycanvas.b1_bind = mycanvas.bind("<ButtonPress-1>", popup, "+")

现在绑定按钮的ID不再是一个全局变量,而是mycanvas作用域变量的一部分

下一步是将取消绑定更改为:

self.canvas.unbind("<ButtonPress-1>", self.b1_bind)

收件人:

self.canvas.unbind("<ButtonPress-1>", self.canvas.b1_bind)

请记住canvasMoveWindow类的一个参数,该类现在通过以下方式调用:

MoveWindow(mycanvas, _item_id, _canvas_xy, popup)

收到时:

def __init__(self, canvas, item, coords, func_bind):

最后一行要更改的内容是:

self.b1_bind = self.canvas.bind("<Button-1>", self.func_bind, "+")

更改为:

self.canvas.b1_bind = self.canvas.bind("<ButtonPress-1>", \
                                       self.func_bind, "+")

相关问题 更多 >