繁体   English   中英

Tkinter - 同时使用滑块旋转图像

[英]Tkinter - rotate image with slider simultaneously

更新 -

tkinter 新手

是否可以同时使用滑块来旋转图片。

我有一个旋转表盘的图像,该图像下方是一个从 0 到 360 列出的滑块。当滑块从 0 移动到 360 时,我希望图像顺时针旋转,当滑块从 360 返回到 0 时逆时针旋转.

使用滑块正确旋转图像

我遇到了一个错误,图像是黑色的。 也许图像被放大了? 抱歉,我是 python 和 tkinter 的新手。

这是 GUI 的外观正确的 GUI

这就是 GUI 现在的样子带有滑块的GUI不正确

这就是 GUI 移除缩略图线缩略图的样子

这是更新的代码

# 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将其转换为标签中显示的PhotoImage Scale 现在有一个命令rotate_image ,它接收缩放值,即度数,然后使用 PIL 创建一个新的旋转图像,然后将其转换为PhotoImage并通过更改其图像显示在标签中.config 现在,当您移动滑块时,图像会随之旋转。

暂无
暂无

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

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