2017-06-03 148 views
0

我有一個python腳本和一個文本文件,其中包含像18:59,19:00,19:02等等的時間(字符串)列表...每個都在不同的行中。我想在文件中的時間是當前時間的情況下更改tkinter中標籤的顏色。Tkinter窗口在運行時關閉

問題是tkinter窗口一旦打開就會關閉,然後一旦一切完成就會再次打開。我嘗試使用update_idletasks。但是這並沒有改變任何東西。下面是所需的代碼結構:

class gui(Frame): 
    def __init__(self, parent, txt2, index): 
    Frame.__init__(self,parent,background="white") 
    self.parent = parent 
    self.txt2 = txt2 
    self.index = index 
    self.initUI() 
    self.initChange() 

def initUI(self) 
    #initial window with white color label 

def initChange(self) 
    while(last line hasnt reached): 
     if(current time = file time) 
      #line for changing label color and self.index++ 
      self.parent.update_idletasks() 
     sleep(30) 

main() 
    #call all the functions required and root, txt file and initial index is passed as arguments for gui functions 

由於我被要求忍受我已經使用了所有的功能,我這樣做,但我忽略了細節。代碼太長。

def dataextract(xlsheet): 
    #take data from xlsheet 
    #called only once in the main() function 

def fileio(x, txt1) 
    #Write the time list after doing some operations 
    #based on data from xlsheet 

def main(): 
    x = datextract(xlsheet) 
    fileio(x, txt) 
    print "File IO competed..." 
    root = Tk() 
    app = gui(root, txt2, 0) 
    root.mainloop() 

if __name__ == "__main__": 
    main() 
+0

什麼是'main()'?而所有的功能/線路都被遺漏了。沒有看到這些,我們不禁感慨。 – SneakyTurtle

+0

你有'主循環'嗎? – Pythonista

+0

@Pythonista是的,我做 – Jake259

回答

0

經過堆棧溢出和許多其他網站的很多帖子後,我得到了我的應用程序工作正常。問題是,python運行在單個線程上。 tkinter窗口最初被分配一個線程,當調用initChange時,線程現在從tkinter中被帶走。我所要做的就是在一個單獨的線程中調用initChange。要這樣做,

class gui(Frame): 
    def __init__(self, parent, txt2, index): 
    Frame.__init__(self,parent,background="white") 
    self.parent = parent 
    self.txt2 = txt2 
    self.index = index 
    self.initUI() 
    thread.start_new_thread(self.initChange,()) 

但這裏有一個小問題。自從我的while循環在指定的時間到達後每秒都會檢查一次後,會有一些延遲。如果有人可以提出一些改進建議,這將非常有幫助。