2017-04-08 187 views
1

我是tkinter的新手,並希望根據顯示的標誌進行UI操作。 基本上,我想關閉一個窗口,打開另一個窗口與當前狀態或刪除文本,並顯示與當前狀態的另一個文本。關閉tkinter的窗口

class App(threading.Thread): 

    def __init__(self): 
     threading.Thread.__init__(self) 
     self.start() 

    def callback(self): 
     self.root.quit() 

    def run(self): 
     self.root = tk.Tk() 
     self.root.protocol("WM_DELETE_WINDOW", self.callback) 

     label = tk.Label(self.root, text="Start Initialization") 
     label.pack() 

     self.root.mainloop() 


class QQQ: 

    def quit(self): 
     self.delete(1.0,END) 



class Appo(threading.Thread): 

    def __init__(self): 
     threading.Thread.__init__(self) 
     self.start() 

    def callback(self): 
     self.root.quit() 

    def run(self): 
     self.root = tk.Tk() 
     self.root.protocol("WM_DELETE_WINDOW", self.callback) 

     label = tk.Label(self.root, text="Initialization ended") 
     label.pack() 

     self.root.mainloop() 


for i in range(100000): 

    time.sleep(0.5) 

    print(i) 

    if(i==1): 

     app = App() 

     time.sleep(1) 

     qqq=QQQ() 

    if(i==10): 

     app=Appo() 
+2

你的問題是什麼? (建議刪除代碼的雙倍間隔) –

+1

在線程函數中,可以使用while循環並檢查可以在主線程中更改的條件。 – Dashadower

+0

我正在考慮實時更改短信。 – CherChuan

回答

0

如果您只想更改標籤的文本,請在標籤上使用config方法。該AppAppoQQQ類,還有for迴路可以組合成一個單一類爲:

import Tkinter as tk #Python 2 
#import tkinter as tk #Python 3 
import threading 
import time 


class App(threading.Thread): 

    def __init__(self): 
     threading.Thread.__init__(self) 
     self.root = tk.Tk() 
     self.start() 

    def callback(self): 
     self.root.quit() 

    def run(self): 
     self.root.protocol("WM_DELETE_WINDOW", self.callback) 

     label = tk.Label(self.root, text="Initial Text") # You can use text="" 
     label.pack() 

     for i in range(100000): 
      time.sleep(0.5) 
      print (i) 
      if i == 1: 
       label.config(text="Start Initialization") 
       time.sleep(1) 
       label.config(text="") 
      if i == 10: 
       label.config(text="Initialization ended") 
     #self.root.mainloop() 

app = App() 
app.root.mainloop() 

這可能是最好使用的Tkinter的after方法對時間延遲,而不是time.sleep()