python - 在 tkinter 中模拟 tail 函数而不卡住 gui

标签 python multithreading tkinter tail

基本上我正在尝试将 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()

最佳答案

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

然后让其他一切保持不变

关于python - 在 tkinter 中模拟 tail 函数而不卡住 gui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20974843/

相关文章:

python - 如何使用 pandas 从目录中的 Excel 工作表中获取每一行值

python - scipy中的偏斜正态分布

java - 我是否需要一个并发集合来通过多个线程将元素添加到列表中?

iphone - 在 iOS 4 (iPhone) 上使用 NSInitationOperation (NSOperation) 和 NSOperationQueue 会导致速度显着下降

框架内的python tkinter框架

python - 更改 xpath selenium python

python - 如何查找和测量图像中的弧长

java - 为什么Java不释放内存

python - 在没有 root 访问权限的情况下安装 Tkinter

python-3.x - Tkinter w.destroy() 阻止/重置窗口大小调整