简体   繁体   English

如何使用相同的按钮停止/暂停和重新启动代码? (tkinter)

[英]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.

相关问题 在python中将pygame与tkinter一起使用时,如何使用同一按钮暂停和取消暂停? - Using pygame with tkinter in Python, how do I pause and unpause with the same button? 如何暂停代码执行,直到我单击 tkinter 按钮? - How to pause code execution until I click tkinter button? 如何在 python 中实现 tkinter 进度条,用户可以在相同的 position 中暂停和重新启动进度条? - How to implement a tkinter progress bar in python where the user can pause and restart the progress bar at the same position it was stopped? 使用 tkinter 为 Python 中的代码添加开始和停止按钮 - Add start and Stop Button for code in Python Using tkinter 在嵌入 tkinter 的按钮按下时暂停/停止 matplotlib 的 animation - Pause/Stop animation of matplotlib on button press embedded in tkinter 使用 tkinter 在按下按钮时重新启动脚本 - Restart script on button press using tkinter 在Tkinter中使用同一按钮启动和停止功能 - Use start and stop function with same button in Tkinter 单击按钮如何重新启动tkinter / python程序? - How to restart tkinter/python program on click of a button? 通过同一窗口中的tkinter按钮启动/暂停pygame - Start/pause pygame via tkinter button in the same window Tkinter - 如何使用停止按钮停止循环? - Tkinter - How to stop a loop with a stop button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM