简体   繁体   English

在tkinter中使用图像旋转标签

[英]Rotate a label with image in tkinter

I am attempting to rotate an label with an image which is on a canvas with tkinter. 我试图用带有tkinter的画布上的图像旋转标签。

I have three images that need rotating (pitch, roll and yaw) and they will eventually rotate according to IMU sensor outputs. 我有三个需要旋转的图像(俯仰,滚动和偏航),它们最终会根据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()

How can I rotate the image labels through tkinter? 如何通过tkinter旋转图像标签?

If you are looking to simple rotate 90, 180 or flip then you can use PIL to transpose . 如果您想简单地旋转90,180或翻转,那么您可以使用PIL进行transpose

See this link for the details: Transpose . 有关详细信息,请参阅此链接: 转置

Here is the important bits just in case link brakes in the future: 以下是重要的部分,以防万一将来制动链接:

Image.transpose(method) Transpose image (flip or rotate in 90 degree steps) Image.transpose(方法)移调图像(以90度步进翻转或旋转)

Parameters: method – One of: 参数:method - 以下之一:

  • PIL.Image.FLIP_LEFT_RIGHT PIL.Image.FLIP_LEFT_RIGHT
  • PIL.Image.FLIP_TOP_BOTTOM PIL.Image.FLIP_TOP_BOTTOM
  • PIL.Image.ROTATE_90 PIL.Image.ROTATE_90
  • PIL.Image.ROTATE_180 PIL.Image.ROTATE_180
  • PIL.Image.ROTATE_270 PIL.Image.ROTATE_270
  • PIL.Image.TRANSPOSE PIL.Image.TRANSPOSE

Returns: Returns a flipped or rotated copy of this image. 返回:返回此图像的翻转或旋转副本。

tkinter doesn't have any support for rotating canvas items. tkinter对旋转画布项目没有任何支持。

From the official tcl/tk documentation (tkinter is a wrapper around a tcl interpreter): 官方的tcl / tk文档 (tkinter是tcl解释器的包装器):

Individual items may be moved or scaled using widget commands described below, but they may not be rotated. 可以使用下面描述的窗口小部件命令来移动或缩放单个项目,但是它们可以不被旋转。

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

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