2017-04-08 186 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()