【问题标题】:Python Pause/Resume ButtonPython 暂停/恢复按钮
【发布时间】:2017-11-19 02:18:50
【问题描述】:

我正在尝试制作一个可以暂停和恢复循环的按钮。

在代码中:

for index in range(10):
    print index
    // Runs until here, and pause
    // Button pressed
    print index + 10
    // Runs until here, and pause
    // Button pressed

在终端:

0
// Button pressed
10
// Button pressed
1
// Button pressed
11
...
9
// Button pressed
19
// Button pressed

有没有一种方法可以通过按钮暂停和恢复循环?

【问题讨论】:

  • 我对你在问什么感到困惑。您是否希望 for 循环在继续之前等待按下按钮?
  • @James 是的,没错。
  • 每个 GUI 框架中的长时间运行循环都会产生问题,因为它会停止其主循环(事件循环)并且看起来像是挂了。所以主要问题是如何运行它而不是停止主循环。如果您将在第二个线程中运行它,那么您可以使用while first_time_pressed == False: pass 来停止循环,并且主线程中的按钮会更改first_time_pressed = True、second_time_pressed = True 等。但是这个while 循环将使用太多的CPU 功率。
  • 到目前为止必须尝试什么?请发布您的代码。
  • 顺便说一句:您可以使用yield 停止功能,按钮可以再次运行功能,并在yield 之后继续运行。

标签: python tkinter


【解决方案1】:

您可以通过在每次单击按钮时调用next() 来使用生成器。

一个小例子:

import tkinter as tk

def plusten(x):
    i = 0
    while i<x:
        yield i
        yield i+10
        i += 1

def next_item():
    if gen:
        try:
            lbl["text"] = next(gen) #calls the next item of generator
        except StopIteration:
            lbl["text"] = "End of iteration" #if generator is exhausted, write an error
    else:
        lbl["text"] = "start loop by entering a number and pressing start loop button"

def start_gen():
    global gen
    try:
        gen = plusten(int(ent.get()))
        lbl["text"] = "loop started with value: " + ent.get()
    except ValueError:
        lbl["text"] = "Enter a valid value"

gen = None

root = tk.Tk()

ent = tk.Entry()
ent.pack()
tk.Button(root, text="start loop", command=start_gen).pack()

tk.Button(root, text="next item", command=next_item).pack()
lbl = tk.Label(root, text="")
lbl.pack()

root.mainloop()

【讨论】:

    【解决方案2】:

    ''' 在代码中导入以下内容 从 tkinter 导入 * 进口时间

    以下程序创建一个带有两个按钮的 tkinter 窗口 (暂停和退出),同时打印一个数字序列 从 1 到 numb(numb=20, 此处用于说明) 可控 打印之间的时间延迟(此处为 2 秒)。按下 暂停按钮停止打印,将按钮文本更改为恢复。 按下恢复按钮一段时间后,打印继续。 要退出打印,请按下退出按钮。 在插图中,打印数字是象征性的。它也可以是 显示具有时间延迟的图像。当需要查看 更详细的图像,显​​示该图像时可以暂停显示 并在需要查看剩余图像时恢复。 ''' 从 tkinter 导入 * 进口时间

    global move,stop 
    move = 1 #this a variable which can take a value 1 or 0
    stop='Initial-Value'  #This is a variable to control the for and while loops.
                          #Assignment of string as variable is optional. It could be
                          #integer as well
    
    def switch():        #controls back and forth switching between Pause and Resume
        global move 
        move = not move
    
    def come_out():      # controls the process to be running or quit
        global stop
        stop='stop'
    
    numb=20
    root = Tk()
    Pause_Button=Button(root,bg='wheat',command=switch)
    Pause_Button.place(relwidth=0.25,relx=0.2,rely=0.2)
    Quit_Button=Button(root,bg='lightblue',text='Quit',command=come_out)
    Quit_Button.place(relwidth=0.25,relx=0.5,rely=0.2)
    time.sleep(2)
    for s in range(numb):
        if stop=='stop':
            break
        stop='continue_while_loop'     
        while s<numb and stop=='continue_while_loop':
            stop='enter_for_loop_again'
            Pause_Button.update()
            if move==1:
                Pause_Button["text"]='Pause'
                print('s2=',s,'\n')
                time.sleep(2)
            else:
                stop='continue_while_loop'
                Pause_Button["text"]='Resume'
            Quit_Button.update()
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多