2014-10-01 27 views
1

我做了一个输入小部件,它获得输入的数字,当他们点击确定(这成为函数重复(n))的秒数),我想每n秒调用一次该函数。点击OK后,我想要一个按钮在屏幕上显示,以防止该功能重复发生。我怎样才能做到这一点?在一定的时间后通话功能,并在按钮上按下按钮,tkinter

这是我有:

def ok_func(): 
    global stop 
    stop = 1 
    timer_pop.destroy() 
    seconds = seconds_entry.get() 
    seconds = int(seconds) 
    stop_timer.grid() 
    while stop == 1: 
     stop_timer.config(highlightbackground=mycolor) 
     background() 
     time.sleep(seconds) 

这是我对停止计时器按钮:

def stopTimer(): 
    global stop 
    stop = 0 
    stop_timer.grid_forget() 

感谢 编辑:

global counter 
counter = 0 

def ok_func(): 
    global stop_timer 
    print('function: "ok" is running now.') 
    global counter 
    counter += 1 
    def stopTimer(): 
     global recur 
     stop_timer.destroy() 
     timer_pop.after_cancel(recur) 
    try: 
     if counter == 1: 
      global time 
      time = int(seconds_entry.get()) 
      timer_pop.destroy() 
      stop_timer = Button(app, text="Stop Timer", command=stopTimer) 
      stop_timer.grid(row=0, column=6, padx=10) 
      #stop_timer.config(highlightbackground=ok_func) 
      global recur 
      print ('function will start again in', time, 'seconds') 
      recur = app.after(1000*time, background) 
     else: 
      print ('function will start again in', time, 'seconds') 
      recur = app.after(1000*time, background) 
      #stop_timer.config(highlightbackground=mycolor) 
    except ValueError: 
     counter = 0 
     print("thats not a number") 

我试着你说了什么,但仍然没有工作。颜色只改变一次,然后停止。此外,我想停止按钮来更改背景背景,但它不起作用。谢谢你的帮助。

+3

不使用'time.sleep' ...你会杀了你的应用程序。使用'tkinter.Widget.after'。见例如http://stackoverflow.com/a/12043756/748858和http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.after-method – mgilson 2014-10-01 23:50:23

回答

1

这是一个完整的例子做了你希望它是什么(我觉得)

创建循环的功能,你选择怎样的情形时常浮现,那么你就可以按下一个按钮杀死它(使用after_cancel所以程序仍然会运行,但不会做任何事情)。

from tkinter import * 

root = Tk() 

global counter 
counter = 0 

def ok(): 
    print('function: "ok" is running now.') 
    global counter 
    counter += 1 
    def stop(): 
     global recur 
     label.destroy() 
     stop_button.destroy() 
     root.after_cancel(recur) 
    try: 
     if counter == 1: 
      global time 
      time = int(entry.get()) 
      label.config(text = 'your only option now is to stop the function') 
      entry.destroy() 
      ok_button.destroy() 
      stop_button = Button(text = 'stop', command = stop) 
      stop_button.pack() 
      global recur 
      print ('function will start again in', time, 'seconds') 
      recur = root.after(1000*time, ok) 
     else: 
      print ('function will start again in', time, 'seconds') 
      recur = root.after(1000*time, ok) 
    except ValueError: 
     counter = 0 
     print('thats not a number') 

label = Label(root, text = 'pick a number of seconds for the function to recur in') 
label.pack() 

entry = Entry(root) 
entry.pack() 

ok_button = Button(root, text = 'Ok', command = ok) 
ok_button.pack() 

root.title('cool recurring function') 
root.mainloop() 

希望这是你想要的,如果不是大喊(并告诉我你想做什么)!

+0

噢,忘了提及,mgilson首先给出了正确的评论,我只是想我会扩展它,并给出一个工作的例子。 – W1ll1amvl 2014-10-02 02:31:34

相关问题