2012-06-28 81 views
2

我是新来的python,我一直在试图找到一个简单的方法来暂停操作while循环,使它有可能从它被暂停的地方再次启动它。我搜索了谷歌的帮助和提示,但我发现的一切似乎都非常复杂。有没有简单的方法来做到这一点?暂停一个无限的while循环与一个按键

我读过你可以使用termios和tkinter。

我正在使用ubuntu。

语言错误

对不起

谢谢!

+1

http://stackoverflow.com/questions/368545/how-can-i-stop-a-while-loop 这可能重复的问题,看看 – doniyor

+0

@doniyor:暂停是不太和停止一样。 – Junuxx

回答

1

这是一个简单的tkinter程序,运行在无限循环中。按空间暂停/取消保留它,并按退出Esc退出。

注意:以下是针对Python 2.x的,如果您使用的是Python 3,请将第一行中的Tkinter更改为tkinter(小写字母t)。

from Tkinter import * 
import time 

class MyLoop(): 
    def __init__(self, root): 
     self.running = True 
     self.aboutToQuit = False 
     self.root = root 
     self.someVar = 0 
     self.root.bind("<space>", self.switch) 
     self.root.bind("<Escape>", self.exit) 

     while not self.aboutToQuit: 
      self.root.update() # always process new events 

      if self.running: 
       # do stuff 
       self.someVar += 1 
       print(self.someVar) 
       time.sleep(.1) 

      else: # If paused, don't do anything 
       time.sleep(.1) 

    def switch(self, event): 
     print(['Unpausing','Pausing'][self.running]) 
     self.running = not(self.running) 

    def exit(self, event): 
     self.aboutToQuit = True 
     self.root.destroy() 

if __name__ == "__main__": 
    root = Tk() 
    root.withdraw() # don't show the tkinter window 
    MyLoop(root) 
    root.mainloop() 

这是一个可能的输出,其中,I手动暂停和取消暂停我退出之前两次该程序。

1 
2 
3 
4 
5 
6 
7 
Pausing 
Unpausing 
8 
9 
10 
11 
12 
13 
14 
15 
16 
Pausing 
Unpausing 
17 
18 
19 
20 
21 
22 
23 
24 
25 
+0

非常感谢,这就是我一直在寻找的! :) – fatninja

1

别有关如何使用一个按键做,但恢复和暂停while循环,你可以使用generator functions,见下面的例子:

i=-1 
def infi(): 
    global i 
    while i<99999999: 
     i+=1 
     yield i 

a=iter(infi()) 

for x in range(6): 
    print(next(a)) 
#loop paused at 5  
print('now it wil start from start 6') 

for x in range(11): 
    print(next(a)) 

输出:

0 
1 
2 
3 
4 
5 
now it wil start from start 6 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
+0

好的,谢谢!事情只是我希望能够在任何给定的时间暂停它,因为它正在循环。 – fatninja

+0

@ user1488138这是'generator function'的作用,他们可以暂停执行,并且可以在他们离开的地方开始执行。 –

+0

@Ashwini:这似乎没有做OP所要求的。用户如何暂停过程? – Junuxx

0

花花公子它是一个while循环...不要在while循环运行,这将阻止了CPU,只是尽量使用定时器...

这里是一个Python 3例让输入来自未显示的窗口,在OSX,Linux操作系统Ubuntu和Windows 8测试

import tkinter as tk 
import time 

class App(): 
    def __init__(self): 
     self.state = 'running' 
     self.cnt = 0 
     self.root = tk.Tk() 
     self.update_clock() 
     self.root.bind("<Key>", self.key) 
     self.root.geometry('1x1+9000+9000') 
     self.root.mainloop() 

    def update_clock(self): 
     now = time.strftime("%H:%M:%S") 
     self.root.after(100, self.update_clock) 
     self.root.focus_set() 
     if(self.state == 'running'): 
      self.cnt = self.cnt + 1 
     if(self.cnt <= 100): 
      print('state: ',self.state,' count:',self.cnt,'%') 
     else: 
      exit() 

    def key(self,event): 
     self.root.focus_force() 
     if(event.keysym == 'Escape'): 
      self.state = 'paused' 
     elif(event.keysym == 'space'): 
      self.state = 'running' 
     print("pressed", repr(event.keysym)) 
     print("state",self.state) 
app=App() 
app.mainloop() 

你可以感谢@saranyan这个...

+0

哦,顺便说一句,你应该可能self.root。update_clock方法中的focus_set(),并且保存该数字的计数或数据为在__init__中声明的另一个var,但仅当(self.state =='paused')时更新: –

0

我永远搜索此解决方案,但事实证明到b真的很简单。我非常喜欢选择模块只适用于Unix系统,但它使事情变得非常简单。

import sys 
import select 
inputSrc = [sys.stdin] 

while True: 

    # Do important stuff 

    # Check if a key was pressed and if so, process input 
    keypress = select.select(inputSrc, [], [], 0)[0] 
    while keypress: 
     for src in keypress: 
      line = src.readline() 
      if not line: 
       inputSrc.remove(src) 
      else: 
       # The "enter" key prints out a menu 
       if line == "\n": 
        print "Test Paused\nOptions: [0]: Quit [any other key]: Resume" 
       elif line.rstrip() == "0": 
        exit(0) 
       elif len(line) >= 1: #any other key 
        print "Resuming..." 
        keypress = None