【问题标题】:Simulating tail function in tkinter without freezing gui在不冻结 gui 的情况下在 tkinter 中模拟尾部功能
【发布时间】:2014-01-07 14:58:01
【问题描述】:

基本上,我正在尝试将 ubuntu 中的 tail 函数实现为 python tkinter 按钮。尾部代码工作正常,但只有当我尝试将它实现到 tkinter 时,我才会遇到整个 gui 冻结的问题。我可以采取几种方法来解决这个问题,但是我想知道解决这个问题的最简单和最快的方法是什么。到目前为止,这是我的代码:

#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()

【问题讨论】:

  • 尝试在follow函数中使用after而不是while True循环。
  • @tobias_k 试过了,好像没成功

标签: python multithreading tkinter tail


【解决方案1】:

围绕我的功能进行了更改。只需要进行一些细微的修改。我摆脱了废话、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)

然后其他的都一样

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2015-04-08
    • 1970-01-01
    • 2021-03-13
    相关资源
    最近更新 更多