繁体   English   中英

在 tkinter 上将图像居中作为画布

[英]Centering an image as canvas on tkinter

canvas = tk.Canvas(root, height=400, width=1100)
image = PhotoImage(file="C:\\Users\\name\\Pictures\\picture.png")
canvas.create_image(0, 0, anchor="center", image=image)
canvas.place(relx=0.5, rely=0.5, anchor="center")
root.resizable(width=False, height=False)
canvas.pack()

我曾尝试将图像制作为我的 GUI 的画布,但它不会将其自身置于应用程序的中心...我已经尝试了所有类型的功能,例如相对于画布的 X 和 Y 以及锚定,但我似乎无法获得它以自我为中心..

您需要使用create_image(x,y,...)来指定画布中间的坐标。 中心是高度/宽度除以二。

from tkinter import *

root = Tk()
HEIGHT = 400
WIDTH = 1100
canvas = Canvas(root, height=HEIGHT, width=WIDTH)
image = PhotoImage(file=r"C:\Users\User\MyImage.png")
canvas.create_image(WIDTH/2, HEIGHT/2, anchor="center", image=image)
root.resizable(width=False, height=False)
canvas.pack()

root.mainloop()

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM