缩放时,tkinter画布上的ARC出现故障

时间:2018-09-02 18:13:27

标签: python-3.x tkinter tkinter-canvas

我在tkinter画布上绘制弧线时遇到问题。 (我正在为scalingscrolling画布使用推荐的方法,请参见我的代码...)

该代码在画布上创建一个弧,其样式为'pieslice'。 起初,一切似乎都正常,但是当我不断放大形状的弯曲边缘时,在某些时候它开始与其他边缘不匹配,最终消失了...

如果我继续放大,其他边缘也会消失...

from tkinter import *

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
cnv = Canvas(root)
cnv.grid(row=0, column=0, sticky='nswe')
cnv.create_arc(20, 20, 250, 250, start=0, extent=30)


def scroll_start(event):
    cnv.configure(cursor='fleur')
    cnv.scan_mark(event.x, event.y)


def scroll_move(event):
    cnv.scan_dragto(event.x, event.y, 1)


def scroll_end(event):
    cnv.configure(cursor='arrow')


def zoom(event):
    if event.delta > 0:
        cnv.scale('all', cnv.canvasx(event.x), cnv.canvasy(event.y), 1.1, 1.1)
    else:
        cnv.scale('all', cnv.canvasx(event.x), cnv.canvasy(event.y), 0.9, 0.9)


cnv.bind('<Button-3>', scroll_start)
cnv.bind('<B3-Motion>', scroll_move)
cnv.bind('<ButtonRelease-3>', scroll_end)
cnv.bind('<MouseWheel>', zoom)

root.mainloop()

是否有解决此问题的方法,还是我逐渐了解tkinter的局限性?感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

这是部分答案,可以衡量缩放似乎崩溃的极限:

添加一个变量来记录scaling_factor,在观察OP描述的现象之前,我可以达到336倍的放大倍数。我推测这可能是浮点精度问题,画布大小限制或其他原因?

from tkinter import *

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
cnv = Canvas(root)
cnv.grid(row=0, column=0, sticky='nswe')
cnv.create_arc(20, 20, 250, 250, start=0, extent=30)

scaling_factor = 1


def scroll_start(event):
    cnv.configure(cursor='fleur')
    cnv.scan_mark(event.x, event.y)


def scroll_move(event):
    cnv.scan_dragto(event.x, event.y, 1)


def scroll_end(event):
    cnv.configure(cursor='arrow')


def zoom(event):
    global scaling_factor
    if event.delta > 0:
        cnv.scale('all', cnv.canvasx(event.x), cnv.canvasy(event.y), 1.1, 1.1)
        scaling_factor *= 1.1
        print(scaling_factor)
    else:
        cnv.scale('all', cnv.canvasx(event.x), cnv.canvasy(event.y), 1/1.1, 1/1.1)
        scaling_factor *= .9
        print(scaling_factor)

cnv.bind('<Button-3>', scroll_start)
cnv.bind('<B3-Motion>', scroll_move)
cnv.bind('<ButtonRelease-3>', scroll_end)
cnv.bind('<MouseWheel>', zoom)

root.mainloop()