【发布时间】:2019-02-27 19:56:13
【问题描述】:
我正在使用 tkinter 开发带有 python 的 GUI。我编写了用于提供缩放功能的代码,例如“放大”、“缩小”、“窗口缩放”、“以前的缩放”、“恢复全缩放”,最后是“平移”。对于“平移”命令,我使用了 canvas.scan_mark() 和 canvas.scan_dragto() 方法。所有提到的命令都可以正常工作,除了当我按“平移”然后“窗口缩放”或尝试绘制选择矩形时,我意外地发现窗口缩放或选择矩形从当前鼠标位置移动了 shift pan 命令的值,尽管我在“pan”命令的末尾解除了所有鼠标事件的绑定。
我尝试了类似xi, yi = canvas.xview()[0], canvas.yview()[0] 然后
canvas.xview_moveto(xi), canvas.yview_moveto(yi)。它将画布视图恢复到其原始位置,但尚未解决问题。此外,它会干扰“恢复全缩放”和“缩放上一个”命令。
请帮我解决这个问题。
非常感谢。
有关问题的简短描述,请参阅下面的代码。这不是我在程序中使用的,但它描述了问题。尝试先按变焦按钮并平移。先按平移按钮再按缩放按钮再试一次,看看有什么不同。
from tkinter import *
root = Tk()
root.resizable(False, False)
frame = Frame(root)
frame.pack(expand=YES, fill=BOTH)
canv = Canvas(frame, bg='white', width=800, height=600)
canv.pack(side=TOP, expand=YES, fill=BOTH)
canv.create_rectangle(100,100,200,200, fill='red', width=3)
canv.create_oval(250,250,450,450, fill='blue', width=3)
canv.create_line(500,500,500,500, fill='white')
def pan():
canv.bind('<Button-1>', startpan)
canv.bind('<B1-Motion>', dragpan)
canv.bind('<ButtonRelease-1>', endpan)
canv.config(cursor='hand1')
def startpan(event):
canv.scan_mark(event.x, event.y)
def dragpan(event):
canv.scan_dragto(event.x, event.y, 1)
def endpan(event):
unbind_events()
def unbind_events():
canv.unbind('<Button-1>')
canv.unbind('<B1-Motion>')
canv.unbind('<ButtonRelease-1>')
canv.config(cursor='arrow')
def zoom_window():
canv.bind('<Button-1>', startzoomwindow)
canv.bind('<B1-Motion>', dragzoomwindow)
canv.bind('<ButtonRelease-1>', endzoomwindow)
def startzoomwindow(event):
global x1, y1
x1, y1 = event.x, event.y
def dragzoomwindow(event):
global rect
x2, y2 = event.x, event.y
rect = canv.create_rectangle(x1, y1, x2, y2, width=2, outline='red')
canv.delete(canv.find_below(rect))
def endzoomwindow(event):
canv.delete(rect)
x, y = 0.5 * (x1 + event.x), 0.5 * (y1 + event.y)
rect_width = abs(event.x - x1)
rect_height = abs(event.y - y1)
canvwidth = canv.winfo_width()
canvheight = canv.winfo_height()
factor = min(canvwidth / rect_width, canvheight / rect_height)
canv.scale(ALL, x, y, factor, factor)
unbind_events()
butnframe = Frame(frame)
butnframe.pack(side=TOP, expand=YES, fill=X)
Button(butnframe, text='Zoom Window', command=zoom_window).pack(side=LEFT)
Button(butnframe, text='Pan', command=pan).pack(side=RIGHT)
【问题讨论】:
-
请出示说明问题的minimal reproducible example。请确保它尽可能小。我们不需要查看所有函数的代码,只需运行程序并查看调用
scan_mark的负面影响即可。您的代码中存在错误,但代码的简单描述不足以让我们对其进行调试。
标签: python canvas tkinter zooming panning