如何解决Tkinter - 同时使用滑块旋转图像
更新 -
tkinter 新手
是否可以同时使用滑块来旋转图片。
我有一个旋转表盘的图像,该图像下方是一个从 0 到 360 列出的滑块。当滑块从 0 移动到 360 时,我希望图像顺时针旋转,当滑块从 0 移动到 360 时逆时针旋转360 到 0。
使用滑块正确旋转图像
我遇到了一个错误,图像是黑色的。也许图像被放大了?抱歉,我是 Python 和 tkinter 的新手。
GUI 的外观如下 Correct GUI
GUI 现在的样子 Incorrect GUI with Slider
这就是删除缩略图行的 GUI 外观THUMBNAIL
这是更新的代码
# import necessary modules
from tkinter import *
from tkinter import ttk
from PIL import Image,ImageTk
root = Tk()
root.title("Gesture Detection Application")
root.geometry("400x320") # set starting size of window
root.maxsize(400,320) # width x height
root.config(bg="#6FAFE7") # set background color of root window
Heading = Label(root,text="Demonstrator Application2",bg="#2176C1",fg='white',relief=RAISED)
Heading.pack(ipady=5,fill='x')
Heading.config(font=("Font",20)) # change font and size of label
image = Image.open("rotate_dial.png")
width,height = image.size
image.thumbnail((width/5,height/5))
photoimage = ImageTk.PhotoImage(image)
image_label = Label(root,image=photoimage,bg="white",relief=SUNKEN)
image_label.image = photoimage
image_label.pack(pady=5)
def rotate_image(degrees):
new_image = image.rotate(-int(degrees))
photoimage = ImageTk.PhotoImage(new_image)
image_label.image = photoimage #Prevent garbage collection
image_label.config(image = photoimage)
w2 = Scale(root,from_=0,to=360,tickinterval= 30,orient=HORIZONTAL,length=300,command = rotate_image)
w2.pack()
w2.set(0)
root.mainloop()
解决方法
您可以使用 PIL(您已经导入)旋转图像。您可以通过添加命令将其链接到 Scale。
image = Image.open("rotate_dial.png")
width,height = image.size
image.thumbnail((width/5,height/5))
photoimage = ImageTk.PhotoImage(image)
image_label = Label(root,image=photoimage,bg="white",relief=SUNKEN)
image_label.pack(pady=5)
def rotate_image(degrees):
new_image = image.rotate(-int(degrees))
photoimage = ImageTk.PhotoImage(new_image)
image_label.image = photoimage #Prevent garbage collection
image_label.config(image = photoimage)
w2 = Scale(root,from_=0,to=360,tickinterval= 30,orient=HORIZONTAL,length=300,command = rotate_image)
不是最初创建 PhotoImage
,而是现在创建 PIL Image
对象。然后它使用高度和宽度以及 thumbnail
函数来替换 subsample
。然后它使用 ImageTk
将其转换为标签中显示的 tkinter PhotoImage
。 Scale现在有一个命令rotate_image
,它接收比例值,即度数,然后使用PIL创建一个新的旋转图像,然后变成PhotoImage
并显示通过使用 .config
更改其图像来在标签中添加。现在,当您移动滑块时,图像会随之旋转。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。