如果您想在画布上缩放一堆线条,您可以使用scale,但是添加到canvas 的所有新内容也必须进行缩放,否则它会开始变得奇怪。你可能真正需要的是数学。特别是这个等式N = Scale * (N - Pn) + Pn。下面是一个简单的绘图界面,带有您请求的按钮。主要它只是一堆存储点和创建线。您感兴趣的部分是这一行:
line[n] = scale * (point - center) + center.
在我的示例代码中,行存储为[x1, y1, x2, y2](行的开头和结尾)。 line 将引用这样的列表。 line[n] 引用该列表中的每个 x 和 y 值,因为 n 递增。话虽这么说:
line[n] = scale * (point - center) + center 真正的意思是从该点减去中心,将其乘以比例数,然后将中心加回去,这样缩放后的点与中心的距离保持不变。
让我们来一遍一遍,这样你就明白了。
黑板:
x1 = 100
center = 200
scale = 2
formula: x1 = scale * (x1 - center) + center
1 : x1 = 2 * (100 - 200) + 200
2 : x1 = 2 * (-100) + 200
3 : x1 = -200 + 200
4 : x1 = 0
旧的x1 距离center 100
现在x1 距离center 200 倍
x1 距离中心scale 比以前远了几倍
代码:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=800, height=600, bg='white')
canvas.pack()
#for storing line canvas ids and line coordinates
line_ids, lines = [], []
#line properties ~ width and fill
global w, f
w, f = 2, 'red'
#draw a line
def draw(event):
if event.type is tk.EventType.ButtonPress:
#store line start coordinate
lines.append([event.x, event.y])
#create a dummy line
line_ids.append(canvas.create_line(*lines[-1], *lines[-1], fill=f, width=w))
elif event.type is tk.EventType.Motion:
#keep deleting and redrawing the current line til you release the mouse
canvas.delete(line_ids.pop())
line_ids.append(canvas.create_line(*lines[-1], event.x, event.y, fill=f, width=w))
elif event.type is tk.EventType.ButtonRelease:
#append the end coordinate to the last line
lines[-1] += [event.x, event.y]
#add mouse events to canvas for drawing functionality
for event in ['<B1-Motion>', '<Button-1>', '<ButtonRelease-1>']:
canvas.bind(event, draw)
#scale lines
def scale_lines(scale):
#remove all canvas references to lines
canvas.delete("all")
line_ids = []
#traverse every line
for i, line in enumerate(lines):
#traverse every point in THIS line
for n, point in enumerate(line):
#toggle between center x and center y depending if point is an x or y
center = canvas.winfo_width()/2 if not n%2 else canvas.winfo_height()/2
#scale this point
line[n] = scale * (point - center) + center
#create a new line with scaled points
line_ids.append(canvas.create_line(*line, fill=f, width=w))
#increase/decrease buttons
tk.Button(root, command=lambda: scale_lines(.5), text='decrease').pack(side='left')
tk.Button(root, command=lambda: scale_lines(2), text='increase').pack(side='left')
root.mainloop()