简体   繁体   中英

Tkinter-Python rotate image animation while button is pressed

I'm trying to animate an image while a button is pressed, otherwise the image will stop

import time
import tkinter
from PIL import Image, ImageTk
from gpiozero import Button
import RPi.GPIO as GPIO
sensor = Button(11)

class SimpleApp(object):
    def __init__(self, master, filename, **kwargs):
        self.master = master
        self.filename = filename
        self.canvas = tkinter.Canvas(master, width=500, height=500)
        self.canvas.pack()
        self.process_next_frame = self.draw().__next__  
        master.after(1, self.process_next_frame)

    def draw(self):

        image = Image.open(self.filename)
        angle = 0
        print(self.process_next_frame)
        while (sensor.is_pressed):
            tkimage = ImageTk.PhotoImage(image.rotate(angle))
            canvas_obj = self.canvas.create_image(250, 250, image=tkimage)
            self.master.after_idle(self.process_next_frame)
            yield
            self.canvas.delete(canvas_obj)
            angle += 1
            angle %= 360
            time.sleep(0.002)


root = tkinter.Tk()
app = SimpleApp(root, 'load.png')
root.mainloop() 

If the while is set as True the image automatically starts rotating but if I add the sensor.is_pressed the image doesn´t appears. Also I try moving the while before angle +=1 but the image only appears and never starts moving even if I press the button

I'm trying to animate an image while a button is pressed, otherwise the image will stop

import time
import tkinter
from PIL import Image, ImageTk
from gpiozero import Button
import RPi.GPIO as GPIO
sensor = Button(11)

class SimpleApp(object):
    def __init__(self, master, filename, **kwargs):
        self.master = master
        self.filename = filename
        self.canvas = tkinter.Canvas(master, width=500, height=500)
        self.canvas.pack()
        self.process_next_frame = self.draw().__next__  
        master.after(1, self.process_next_frame)

    def draw(self):

        image = Image.open(self.filename)
        angle = 0
        print(self.process_next_frame)
        while (sensor.is_pressed):
            tkimage = ImageTk.PhotoImage(image.rotate(angle))
            canvas_obj = self.canvas.create_image(250, 250, image=tkimage)
            self.master.after_idle(self.process_next_frame)
            yield
            self.canvas.delete(canvas_obj)
            angle += 1
            angle %= 360
            time.sleep(0.002)


root = tkinter.Tk()
app = SimpleApp(root, 'load.png')
root.mainloop() 

If the while is set as True the image automatically starts rotating but if I add the sensor.is_pressed the image doesn´t appears. Also I try moving the while before angle +=1 but the image only appears and never starts moving even if I press the button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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