在 tkinter 中旋转带有图像的标签
Rotate a label with image in tkinter
我正在尝试使用 tkinter 旋转 canvas 上带有图像的标签。
我有三个图像需要旋转(俯仰、滚动和偏航),它们最终会根据 IMU 传感器输出进行旋转。
import tkinter as tk
from tkinter import PhotoImage
from PIL import Image
root = tk.Tk()
root.title('Pitch/Roll/Yaw Simulator')
# pick image file
fname = "PRY_Diag_Dials.png"
bg_image = tk.PhotoImage(file=fname)
# get the width and height of the image
w = bg_image.width()
h = bg_image.height()
# size the window so the image will fill it
root.geometry("%dx%d+50+30" % (w, h))
cv = tk.Canvas(root, width=w, height=h)
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=bg_image, anchor='nw')
#add images
pitch = tk.PhotoImage(file="Pitch2.gif")
pitch_lbl = tk.Label(cv, image=pitch,bg="white")
pitch_lbl.image = pitch
pitch_lbl.place(x=60, y=180)
roll = tk.PhotoImage(file="Roll2.gif")
roll_lbl = tk.Label(cv, image=roll,bg="white")
roll_lbl.image = roll
roll_lbl.place(x=325, y=180)
yaw = tk.PhotoImage(file="Yaw2.gif")
yaw_lbl = tk.Label(cv, image=yaw,bg="white")
yaw_lbl.image = yaw
yaw_lbl.place(x=590, y=180)
root.mainloop()
如何通过 tkinter 旋转图像标签?
如果您希望简单地旋转 90 度、180 度或翻转,则可以使用 PIL
到 transpose
。
有关详细信息,请参阅此 link:Transpose。
以下是重要的部分,以防万一 link 将来刹车:
Image.transpose(方法)
转置图像(翻转或旋转 90 度)
参数:方法 - 其中之一:
- PIL.Image.FLIP_LEFT_RIGHT
- PIL.Image.FLIP_TOP_BOTTOM
- PIL.Image.ROTATE_90
- PIL.Image.ROTATE_180
- PIL.Image.ROTATE_270
- PIL.Image.转置
Returns:Returns 此图像的翻转或旋转副本。
tkinter 不支持旋转 canvas 项。
来自 official tcl/tk documentation(tkinter 是 tcl 解释器的包装器):
Individual items may be moved or scaled using widget commands described below, but they may not be rotated.
我正在尝试使用 tkinter 旋转 canvas 上带有图像的标签。
我有三个图像需要旋转(俯仰、滚动和偏航),它们最终会根据 IMU 传感器输出进行旋转。
import tkinter as tk
from tkinter import PhotoImage
from PIL import Image
root = tk.Tk()
root.title('Pitch/Roll/Yaw Simulator')
# pick image file
fname = "PRY_Diag_Dials.png"
bg_image = tk.PhotoImage(file=fname)
# get the width and height of the image
w = bg_image.width()
h = bg_image.height()
# size the window so the image will fill it
root.geometry("%dx%d+50+30" % (w, h))
cv = tk.Canvas(root, width=w, height=h)
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=bg_image, anchor='nw')
#add images
pitch = tk.PhotoImage(file="Pitch2.gif")
pitch_lbl = tk.Label(cv, image=pitch,bg="white")
pitch_lbl.image = pitch
pitch_lbl.place(x=60, y=180)
roll = tk.PhotoImage(file="Roll2.gif")
roll_lbl = tk.Label(cv, image=roll,bg="white")
roll_lbl.image = roll
roll_lbl.place(x=325, y=180)
yaw = tk.PhotoImage(file="Yaw2.gif")
yaw_lbl = tk.Label(cv, image=yaw,bg="white")
yaw_lbl.image = yaw
yaw_lbl.place(x=590, y=180)
root.mainloop()
如何通过 tkinter 旋转图像标签?
如果您希望简单地旋转 90 度、180 度或翻转,则可以使用 PIL
到 transpose
。
有关详细信息,请参阅此 link:Transpose。
以下是重要的部分,以防万一 link 将来刹车:
Image.transpose(方法) 转置图像(翻转或旋转 90 度)
参数:方法 - 其中之一:
- PIL.Image.FLIP_LEFT_RIGHT
- PIL.Image.FLIP_TOP_BOTTOM
- PIL.Image.ROTATE_90
- PIL.Image.ROTATE_180
- PIL.Image.ROTATE_270
- PIL.Image.转置
Returns:Returns 此图像的翻转或旋转副本。
tkinter 不支持旋转 canvas 项。
来自 official tcl/tk documentation(tkinter 是 tcl 解释器的包装器):
Individual items may be moved or scaled using widget commands described below, but they may not be rotated.