2014-01-07 38 views
1

基本上我試圖從ubuntu實現尾巴功能到python tkinter按鈕。尾部代碼工作正常,但只有當我嘗試將其實現到tkinter時,我遇到了整個GUI凍結的問題。有幾種方法可以解決這個問題,但我想知道什麼是最簡單快速的方法來解決這個問題。這裏是我的代碼到目前爲止:在沒有凍結gui的情況下在tkinter中模擬尾巴功能

#gui code 

from tkinter import * 
from tkinter import filedialog 
from tkinter import Tk, Button 
from subprocess import Popen 
import tkinter 
import os 
import time 

master = Tk() 
root = tkinter.Tk() 

root.attributes("-topmost",True) 
master.withdraw() 
root.lift() 

def userpass(): 
    os.startfile('config.ini') 


def follow(thefile): 
    thefile.seek(0,2)  # Go to the end of the file 
    while True: 
     line = thefile.readline() 
     if not line: 
      time.sleep(1) # Sleep briefly 
      continue 
     yield line 

def crap(tex): 
    loglines = follow(open("jessica.gw2.log")) 
    for line in loglines: 
     #print(line) 
     tex.insert(tkinter.END, line) 
     tex.see(tkinter.END) 


def cbc(tex): 
    return lambda : crap(tex) 

x = (root.winfo_screenwidth() - root.winfo_reqwidth())/1.1  
y = (root.winfo_screenheight() - root.winfo_reqheight())/20 
root.geometry("+%d+%d" % (x, y)) 
tex = tkinter.Text(root) 
tex.pack(side=tkinter.BOTTOM) 

b = Button(root, text="Set your Options ", command=userpass) 
o = Button(root, text="Press to view logs", command = cbc(tex)) 
b.pack() 
o.pack() 


mainloop() 
+0

嘗試在'follow'函數中使用'after'而不是'while True'循環。 –

+0

@tobias_k試過,似乎沒有解決 – ark

回答

0

改變我的功能。只需稍作修改即可。我擺脫廢話,CBC和後續的功能,然後我改變了這個按鈕

o = Button(root, text="Start monitoring Log file", command = viewlogfile) 

,使其調用這個函數,我添加

def viewlogfile(): 
    f = open('insertfilenamehere', "r") 
    text = f.read() 
    tex.insert(tkinter.END, text) 
    tex.see(tkinter.END) 
    root.after(3000,viewlogfile) 

,然後離開一切同

相關問題