[英]Python Tkinter coords function not moving canvas objects inside loop
[英]Is there a Python tkinter function that makes a drawing’s coords on a certain ratio on the canvas widget?
我是 tkinter(python) 的初学者,我想做的是让一行文本在 canvas 上保持相同的坐标比。例如,我想要一行文本留在中间。 是否有任何 tkinter 文本参数使其在不运行 while 循环的情况下保持一定比例? 我想要最小的时间复杂度。
您的 GUI 可以将 function 绑定到 Canvas <Configure>
事件,该事件会在 Canvas 更改大小时触发。 下面是一个简单的例子。
还有一个Canvas.scale
方法会改变 canvas 对象的位置和大小。 文本可能会移动但不会改变大小。
import tkinter as tk
root = tk.Tk()
# Create a canvas that will change size with the root window.
canvas = tk.Canvas( root, width = 600, height = 400, borderwidth = 2,
relief = tk.SOLID )
canvas.grid( sticky = 'nsew', padx = 5, pady = 5 )
root.grid_columnconfigure( 0, weight = 1 )
root.grid_rowconfigure( 0, weight = 1 )
text = canvas.create_text( 50, 50, text = 'Test Text' )
def on_canvas_config( event ):
""" This is bound to the canvas <Configure> event.
It executes when the canvas changes size.
The event is an object that contains the new width and height.
"""
x = max( 25, event.width // 2 ) # x >= 25
y = max( 12, event.height // 8 ) # y >= 12
canvas.coords( text, x, y ) # Set the canvas coordinates
canvas.bind( '<Configure>', on_canvas_config )
root.mainloop()
可以编写on_canvas_configure
function 来修改Canvas 中任何对象的坐标以满足您的要求。 我从未尝试过使用Canvas.scale
方法,但如果有许多对象要在 canvas 上重新定位,这可能值得探索。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.