[英]What units does the scaling method work in tkinter canvas?
Trying to figure out how the scaling method in the Tkinter Canvas package works.试图弄清楚 Tkinter Canvas 包中的缩放方法是如何工作的。
import tkinter
root = tkinter.Tk()
root.geometry("1200x800")
root.resizable(False,False)
root.title("Examples")
myCanvas = tkinter.Canvas(root, bg="white", height=700, width=1000)
myCanvas.create_rectangle(0,200,300,300, tags="myTag")
myCanvas.scale("myTag", 8200,1200,0.99,0.99)
myCanvas.create_rectangle(0,400,300,300, tags="myTag2")
myCanvas.move("myTag2",200,0)
input_elem = tkinter.Entry(root,width=50)
input_elem.grid(row=1, column=0)
btn = tkinter.Button(root,width=50, text="here", height=5)
btn.grid(row=1, column=2)
myCanvas.grid(row=0, column=0)
root.mainloop()
in the documentation I found this:在文档中我发现了这个:
.scale(tagOrId, xOffset, yOffset, xScale, yScale) Scale all objects according to their distance from a point P=(xOffset, yOffset).
.scale(tagOrId, xOffset, yOffset, xScale, yScale) 根据与点 P=(xOffset, yOffset) 的距离缩放所有对象。 The scale factors xScale and yScale are based on a value of 1.0, which means no scaling.
缩放因子 xScale 和 yScale 基于值 1.0,这意味着没有缩放。 Every point in the objects selected by tagOrId is moved so that its x distance from P is multiplied by xScale and its y distance is multiplied by yScale.
移动由 tagOrId 选择的对象中的每个点,使其与 P 的 x 距离乘以 xScale,其 y 距离乘以 yScale。
This method will not change the size of a text item, but may move it.
此方法不会更改文本项的大小,但可以移动它。
Based on this I would have expected the .scale()
to work in the same units as the canvas (by default pixels) but it seems like it doesn't.基于此,我本来希望
.scale()
以与画布相同的单位(默认像素)工作,但似乎并非如此。 My xOffset
value is fairly large and the rectangle moved only very little.我的
xOffset
值相当大,矩形只移动了很少。 So I created a second rectangle to compare and realized it's scaling based off of the width of the canvas, so this :所以我创建了第二个矩形来比较并意识到它是根据画布的宽度进行缩放的,所以这个:
myCanvas.scale("myTag", (20*0.99)*1000,1200,0.99,0.99)
myCanvas.move("myTag2",(0.99*200),0)
equals to the same xOffset.等于相同的 xOffset。 Why is is the scaling a factor of 10 though?
为什么缩放比例是 10 倍? Shouldn't
(200*0.99)*1000
in the scaling method equal to 0.99*200
in the move method?缩放方法中的
(200*0.99)*1000
不应该等于移动方法中的0.99*200
吗? Or can someone point me to a more detailed documentation?或者有人可以指点我更详细的文档吗?
I think the best way to understand Canvas move and scale is to see them in action.我认为了解 Canvas 移动和缩放的最佳方式是查看它们的实际效果。
Here is a small interactive demo that may help.这是一个小型交互式演示,可能会有所帮助。
It draws a small rectangle (square) at origin.它在原点绘制一个小矩形(正方形)。
Control for moving (pick and place) the square is single Button-1 click用于移动(拾取和放置)方块的控制是单次 Button-1 单击
Control for scale is via keyboard (Up, Right) = grow (Down and Left) = shrink比例控制是通过键盘(上、右)= 增长(下和左)= 缩小
Run the code, pick up the square and place it near canvas center.运行代码,拿起正方形并将其放在画布中心附近。
Make it grow by pressing and holding Up or Right key.通过按住向上或向右键使其增长。 Now make it shrink by pressing and holding Down or Left key.
现在通过按住向下或向左键使其缩小。
Try moving mouse pointer inside and outside of square while changing scale.尝试在更改比例时在正方形内外移动鼠标指针。
Note how the square moves away from your mouse pointer when growing and moves toward your mouse pointer when shrinking.注意正方形在增长时如何远离鼠标指针,在缩小时如何向鼠标指针移动。
Exactly what one would expect from objects approaching or receding.正是人们对接近或后退的物体的期望。
You can change scale and move square at the same time.您可以同时更改比例和移动正方形。
This works with objects line, polygon, rectangle and oval.这适用于对象线、多边形、矩形和椭圆形。
import tkinter as tk
class moveScale(tk.Tk):
def __init__(self):
super().__init__()
# Remove various paddings so the entire canvas is visible
self.canvas = tk.Canvas(
self, highlightthickness = 0, borderwidth = 0,
cursor = "crosshair")
self.canvas.grid(sticky = tk.NSEW)
self.xx = self.yy = 0 # Store for dynamic mouse pointer position
self.con = False # Flag controls move on|off
self.tag = "myTag"
self.sU = 1.005 # Scale up (growth)
self.sD = 1 / self.sU # Scale down (shrink)
self.canvas.create_rectangle(
0, 0, 40, 40, fill = "", tags = self.tag)
# Mouse and Keyboard bindings
self.canvas.bind("<Motion>", self.mover)
self.canvas.bind("<Button-1>", self.swap)
self.canvas.event_add("<<BIG>>", "<Up>", "<Right>")
self.canvas.event_add("<<GIB>>", "<Left>", "<Down>")
self.canvas.bind("<<BIG>>", self.big)
self.canvas.bind("<<GIB>>", self.small)
# Keyboard input only works when canvas has the focus
self.canvas.focus_force()
# Movement control
def swap(self, ev):
self.con = self.con == False
def mover(self, ev):
x, y = self.canvas.canvasx(ev.x), self.canvas.canvasy(ev.y)
if self.con:
self.canvas.move(self.tag, x - self.xx, y - self.yy)
self.xx, self.yy = x, y
def big(self, ev):
self.canvas.scale(self.tag, self.xx, self.yy, self.sU, self.sU)
def small(self, ev):
self.canvas.scale(self.tag, self.xx, self.yy, self.sD, self.sD)
main = moveScale()
main.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.