简体   繁体   English

是否有 Python tkinter function 在 canvas 小部件上按一定比例绘制图形坐标?

[英]Is there a Python tkinter function that makes a drawing’s coords on a certain ratio on the canvas widget?

I'm a first timer at tkinter(python) and what I want to do is to make a line of text stay on the same coords ratio on the canvas. For example, I want a line of text to stay in the middle.我是 tkinter(python) 的初学者,我想做的是让一行文本在 canvas 上保持相同的坐标比。例如,我想要一行文本留在中间。 Is there any tkinter text parameters that make it stay in a certain ratio without running a while loop?是否有任何 tkinter 文本参数使其在不运行 while 循环的情况下保持一定比例? I want minimal time complexity.我想要最小的时间复杂度。

Your GUI can have a function bound to the Canvas <Configure> event that fires whenever the Canvas changes size.您的 GUI 可以将 function 绑定到 Canvas <Configure>事件,该事件会在 Canvas 更改大小时触发。 A simple example below.下面是一个简单的例子。

There is also a Canvas.scale method which will change the location and size of canvas objects.还有一个Canvas.scale方法会改变 canvas 对象的位置和大小。 Text may move but will not change size.文本可能会移动但不会改变大小。

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()

The on_canvas_configure function can be written to amend the coords of any objects in the Canvas to meet your requirements.可以编写on_canvas_configure function 来修改Canvas 中任何对象的坐标以满足您的要求。 I've never tried to use the Canvas.scale method but this may be worth exploring if there are many objects to reposition on the canvas.我从未尝试过使用Canvas.scale方法,但如果有许多对象要在 canvas 上重新定位,这可能值得探索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Tkinter coords函数不会在循环内移动画布对象 - Python Tkinter coords function not moving canvas objects inside loop 在python tkinter画布中更改线的坐标 - Change coords of line in python tkinter canvas 做w.coords时,未定义使矩形的Python tkinter函数 - Python tkinter function that makes a rectangle does not define when doing w.coords Python多处理工程图到Tkinter画布 - Python Multiprocessing Drawing to Tkinter Canvas 尝试使用 Canvas.coords() 修改对象坐标时,python 的 tkinter 中出现“_tkinter.TclError: bad screen distance” - "_tkinter.TclError: bad screen distance" in python's tkinter when trying to modify object coordinates with Canvas.coords() Tkinter:在画布底部设置0,0个坐标 - Tkinter: Set 0, 0 coords on the bottom of a canvas Python Tkinter - 如何在 Canvas 中包含小部件? - Python Tkinter - How to contain a widget within a Canvas? 为什么使用Python 3将画布小部件导入Tkinter的速度很慢? - Why the canvas widget into Tkinter with Python 3 is slow? Python Tkinter框架画布,小部件条目大小调整 - Python tkinter frame canvas, widget Entry resize 如何从窗口小部件的函数返回值,并将其传递给Tkinter,Python中的另一个窗口小部件的函数 - How to return value from a widget`s function, and pass it to another widget's function in Tkinter, Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM