无需冻结图形用户界面在tkinter中模拟尾部功能

2024-03-29 02:12:01 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我正在尝试将tail函数从ubuntu实现为pythontkinter按钮。尾部代码工作得很好,但只有当我试图将其实现到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()

Tags: 方法代码fromimporttkinterdeflinegui
1条回答
网友
1楼 · 发布于 2024-03-29 02:12:01

改变了我的功能。只是做了些小小的修改。我去掉了crap,cbc和follow函数,然后我改变了这个按钮

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

所以它调用我添加的这个函数

^{pr2}$

然后把其他的东西都放了

相关问题 更多 >