如何缩放tkinter canvas窗口而不更改对象 [英] How do I scale tkinter canvas window without changing objects
问题描述
如何缩放Tkinter画布本身。我创建一个应用程序,它会更加优雅(也就是说更可读),如果我可以绘制tkinter画布上的工程单位(英寸)的东西,只是缩放到窗口的画布。附加的代码是非常简化的我想要的,但我相信它得到了点。我想应用比例因子一次,而不是每次我添加一个对象到画布。我想画布代表36英寸(或任何)和缩放,使36英寸是1800像素宽(或任何窗口宽度)。一旦建立,这个比例因子不会改变。
(我知道如果self.canvas.coords(thing)[2]> canvaswidth将不得不改变)。
How do I scale a Tkinter canvas itself. I am creating an application where it would be far more elegant ( that is to say much more readable) if I can plot things on a tkinter canvas in engineering units (inches) and simply scale the canvas to the window. The attached code is quite over simplified for what I want to to but I believe that it gets the point across. I would like to apply the scale factor once instead of each and every time I add an object to the canvas. I would like the canvas to represent 36 inches (or whatever) and be scaled so that 36 inches is 1800 pixels wide (or whatever window width). Once established this scale factor would not change. (I know that if self.canvas.coords(thing)[2]>canvaswidth will have to change as well).
我看到这个问题在不同的形式,但我没有找到一个答案,不涉及缩放所有widgits在画布上。我认为有一个全局的比例因子,它洒在它的所有地方,我使用一个画布坐标。
I have seen this asked in different forms but I have not found an answer that does not involve scaling all the widgits on the canvas. I think it would be rather in-elegant to have a global scale factor and sprinkle it everywhere that I used a canvas coordinate.
#AP moving machine
from Tkinter import *
MagicNumber=0.3125
MN=MagicNumber*7
PunchPositions=([MN*1,2],[MN*2,4],[MN*3,2],[MN*4,4])
PiercePositions=([MN*5,2],[MN*6,4],[MN*7,2],[MN*8,4])
class mover():
def __init__(self,root,canvas,things):
self.things=things
self.canvas=canvas
self.root=root
self.root.after(100,self.move)
self.counter=0
def addoval(self,x,y):
r=0.125
self.things.append(canvas.create_oval(x-r,y-r,x+r,y+r,fill='green'))
def addpierce(self,x,y):
r=0.0625
self.things.append(canvas.create_oval(x-r,y-r,x+r,y+r,fill='Black'))
def move(self):
for thing in self.things:
canvas.move(thing,1.250)
canvaswidth=canvas.winfo_width()
for thing in self.things:
if self.canvas.coords(thing)[2]>canvaswidth :
self.canvas.delete(thing)
self.things.remove(thing)
root.after(100,self.move)
self.counter=self.counter+1
for x,y in PunchPositions:
self.addoval(x,y)
for x,y in PiercePositions:
self.addpierce(x,y)
#
root =Tk()
canvas=Canvas(root,width=1800,height=200)
parts=[]
def AddPierce(x,y):
radius=0.125
x1=x-radius
x2=x+radius
y1=y-radius
y2=y+radius
return canvas.create_oval(x1,y1,x2,y2,fill='Black')
def AddPunch(x,y):
radius=0.250
x1=x-radius
x2=x+radius
y1=y-radius
y2=y+radius
return canvas.create_oval(x1,y1,x2,y2,fill='Blue')
for x,y in PunchPositions:
AddPunch(x,y)
for x,y in PiercePositions:
AddPierce(x,y)
for l in range(1,12):
canvas.create_line(l*MagicNumber*4,0,l*MagicNumber*4,6)
canvas.pack(fill=BOTH)
mover(root,canvas,parts)
root.mainloop()
推荐答案
您无法在不修改对象本身的情况下缩放画布中的项目。画布提供的唯一功能是 scale
方法,但它可以通过更改项目的实际坐标来工作。
You cannot scale the items in a canvas without modifying the objects themselves. The only feature that the canvas supplies is the scale
method, but it works by changing the actual coordinates of the items.
这篇关于如何缩放tkinter canvas窗口而不更改对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!