2014-01-30 55 views
0
import sys 
import time 
from tkinter import * 
from tkinter import messagebox 

Gui = Tk() 
Gui.title("Exercise Timer") 

def jesse(derp): 
    x=derp 
    test = 0 
    while x>=0 and x<=derp: 
     if test == 0: 
      Gui.after(1000,makeLabel(x)) 
      x= x-1 
      if x==0: 
       test = 1 
     if test == 1: 
      if x == 0: 
       Gui.after(1000,makeLabel("brk pls")) 
       x=x+1 
      else:  
       Gui.after(1000,makeLabel(x)) 
       x=x+1 

def makeLabel(texts): 
    Gui.update() 
    newlab = Label(text=texts).pack() 

def stop(): 
    Gui.destroy() 

mbutton = Button(text="10 seconds",command = lambda: jesse(10)).pack() 
mbutton = Button(text="20 seconds",command = lambda: jesse(20)).pack() 
mbutton = Button(text="30 seconds",command = lambda: jesse(30)).pack() 
mbutton = Button(text="quit",fg="red",command = lambda: stop()).pack() 

Gui.mainloop() 
sys.exit() 

對不起,如果這是一個愚蠢的問題,我剛開始學習Python。基本上我的計劃是從一個數字倒數,然後每1秒計數一次顯示下一個數字。除了當我關閉它時,它一切正常。它關閉,但函數jesse()繼續前進,出現錯誤後會彈出一個窗口,只顯示下一個數字,即使我點擊了退出按鈕或窗口x按鈕。在Python GUI更新中遇到了一些麻煩

回答

0

import sys 
import time 
from Tkinter import * 
import tkMessageBox 

Gui = Tk() 
Gui.title("Exercise Timer") 

def jesse(derp): 
    x=derp 
    test = 0 
    while x>=0 and x<=derp: 
     if test == 0: 
      Gui.after(1000,makeLabel(x)) 
      x= x-1 
      if x==0: 
       test = 1 
     if test == 1: 
      if x == 0: 
       Gui.after(1000,makeLabel("brk pls")) 
       x=x+1 
      else:  
       Gui.after(1000,makeLabel(x)) 
       x=x+1 

def makeLabel(texts): 
    try: 
     Gui.update() 
     newlab = Label(text=texts).pack() 
    except TclError: 
     pass 

def stop(): 
    Gui.destroy() 

mbutton = Button(text="10 seconds",command = lambda: jesse(10)).pack() 
mbutton = Button(text="20 seconds",command = lambda: jesse(20)).pack() 
mbutton = Button(text="30 seconds",command = lambda: jesse(30)).pack() 
mbutton = Button(text="quit",fg="red",command = lambda: stop()).pack() 

Gui.mainloop() 
sys.exit() 

我添加了一個嘗試,除非塊,使它停止給人一種錯誤,我知道它不是一個非常整潔的修復,但我爲什麼你的代碼甚至給不明一個錯誤。 我希望這可以幫助對不起,如果它不