[英]How to stop/pause and restart the code using same button? (tkinter)
This is a photo slideshow code its just a skeleton I want use buttons to start or stop the code from slide show.这是一个照片幻灯片代码,它只是一个骨架,我想使用按钮来启动或停止幻灯片中的代码。
# import required modules
import tkinter as tk
from tkinter import *
from PIL import Image
from PIL import ImageTk
# This here is to adjust window
root=tk.Tk()
root.geometry("200x200")
# loading the images
img=ImageTk.PhotoImage(Image.open("photo1.png"))
img2=ImageTk.PhotoImage(Image.open("photo2.png"))
img3=ImageTk.PhotoImage(Image.open("photo3.png"))
img4=ImageTk.PhotoImage(Image.open("photo4.png"))
l=Label()
l.pack()
# using recursion to slide to next image
x = 1
# function to change to next image
def move():
global x
if x == 4:
x = 1
if x == 1:
l.config(image=img)
elif x == 2:
l.config(image=img2)
elif x == 3:
l.config(image=img3)
x = x+1
root.after(2000, move)
# calling the function please refer the indents
btn_1 = Button(root, text="start", command=move)
btn_1.pack()
btn_2=Button(root,text="start", command=pause))
btn_2.pack()
root.mainloop()
Here's runnable code that illustrates how to do it.这是说明如何执行此操作的可运行代码。 It uses several global variables to keep track of the current state of the slide show that the various functions use to determine what needs to be be done.
它使用几个全局变量来跟踪幻灯片放映的当前状态,各种函数使用这些变量来确定需要做什么。
It simplies the logic you had for determining the next image to display by adding one to then current image index and the applying the modulo %
the number of images in show which forces the index back to 0
whenever it exceeds the number of image that there are — which can all be done with a single statement: cur_img = (cur_img+1) % len(slides)
.它简化了您确定要显示的下一个图像的逻辑,方法是将一个添加到当前图像索引并应用模数
%
显示的图像数量,这会在索引超过存在的图像数量时强制返回0
— 这一切都可以通过一条语句完成: cur_img = (cur_img+1) % len(slides)
。
You could get rid of the most of the globals by defining a SlideShow
class that encapsulated the state variables and defined the related functions that manipulate them.您可以通过定义一个封装状态变量并定义操作它们的相关函数的
SlideShow
类来摆脱大部分全局变量。
from pathlib import Path
from PIL import Image
from PIL import ImageTk
import tkinter as tk
root = tk.Tk()
root.geometry("500x500")
after_id = None
cur_img = 0
paused = True
image_folder = Path('./photos')
slides = [ImageTk.PhotoImage(Image.open(filename))
for filename in image_folder.glob('*.png')]
def slide_show():
"""Change to next image (wraps around)."""
global after_id, cur_img, paused
if not paused:
cur_img = (cur_img+1) % len(slides)
lbl.config(image=slides[cur_img])
after_id = root.after(2000, slide_show)
def start():
global after_id, cur_img, paused
paused = False
if after_id: # Already started?
root.after_cancel(after_id)
after_id = None
slide_show()
def pause():
global after_id, cur_img, paused
paused = True
lbl = tk.Label(image=slides[cur_img])
lbl.pack()
btn_1 = tk.Button(root, text="start", command=start)
btn_1.pack()
btn_2 = tk.Button(root, text="pause", command=pause)
btn_2.pack()
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.