我对python编程还很陌生,所以我想创建一个程序,该程序的某些表盘带有背景图像,顶部有一些图像,这些图像将根据某些IMU传感器的输出进行旋转。
到目前为止,我已经添加了背景,并使用tkinter创建了带有照片的标签。但是,显示的背景为灰色(原始文件具有透明背景)。
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(width=w, height=h)
cv.pack(side='top', fill='both', expand='yes')
cv.create_image(0, 0, image=bg_image, anchor='nw')
# add canvas text
cv.create_text(15, 20, text="Pitch/Roll/Yaw Simulator", fill="white", anchor='nw')
# add button widgets
btn1 = tk.Button(cv, text="Initialise")
btn1.pack(side='left', padx=10, pady=5, anchor='sw')
btn2 = tk.Button(cv, text="Quit", command=root.destroy)
btn2.pack(side='left', padx=10, pady=5, anchor='sw')
#add image labels
pitch = tk.PhotoImage(file="Pitch.gif")
w1 = tk.Label(cv, image=pitch).pack()
root.mainloop()
是否可以在图像标签上获得透明背景,所以可以将其覆盖在背景上?如果我想根据传感器输出旋转图像,tkinter是最好的选择吗?