簡體   English   中英

Python Tkinter更新文本小部件

[英]Python Tkinter updating a Text Widget

因此,我是Tkinter的新手,我正在嘗試向我編寫的一個文件尾程序添加圖形界面。 從命令行的角度來看,我可以正常工作,但是當您初次加載文件或添加行時,無法在應用程序上更新文本框。 我看到它調用tk,但是沒有按預期更新。 下面是代碼:

from sys import argv
import threading
import tkinter


class MyTkApp(threading.Thread):
    def __init__(self):
        self.root = tkinter.Tk()
        self.root.title("PyTail V 1.0")
        self.s = tkinter.StringVar()
        self.s.set('Begging Tail of File')
        self.text_window = tkinter.Text(self.root, height=20, width=80)
        self.text_window.insert(tkinter.END, self.s.get())
        self.text_window.pack()
        self.root.update()
        threading.Thread.__init__(self)


def run(self):
    self.root.mainloop()


def get_current_line_count(lines):
    lines = lines.count("\n")
    return lines


def get_file(tail_name):
    file = open(tail_name, 'r')
    lines = file.read()
    file.close()
    return lines


def print_lines(lines, begin_line, tail_app):
    split_lines = lines.split("\n")
    for num in range(begin_line, len(split_lines)-1):
        #  print(split_lines[num])
        tail_app.s.set(split_lines[num])



def correct_args(argv):
    if not len(argv) == 2:
        return False
    else:
        return True


def update_window(current_lines, tail_app):
    try:
        file_lines = get_file(argv[1])
        new_lines = get_current_line_count(file_lines)
        if new_lines > current_lines:
            print_lines(file_lines, current_lines, tail_app)
            current_lines = new_lines

    except (KeyboardInterrupt, SystemExit):
            print("Now Exiting.....")
    return current_lines


if correct_args(argv):
    file_lines = get_file(argv[1])
    current_lines = get_current_line_count(file_lines)
    app = MyTkApp()
    app.start()
    print_lines(file_lines, 0, app)
    x = True
    while x:
        current_lines = update_window(current_lines, app)
else:
    print("You must supply the name of a file")

所以我要解決所有這些錯誤。 因為Tk甚至被驅動,所以它總是會進入該循環,而不給我我期望的結果。 我通過從頭開始並使用.after方法來解決此問題。 以下是我的修訂。 總體而言,對於python和GUI而言,這是一次非常不錯的學習體驗。

from sys import argv
import tkinter as tk


text = tk.Text
current_line = 0
file_lines = []
pause_status = False


def pause_updates():
    global pause_status
    if pause_status:
        pause_status = False
        root.title("Pytail v1.0 - Watching File")
    else:
        pause_status = True
        root.title("Pytail v1.0 - Paused")

 def get_current_line_count(lines):
    lines = lines.count("\n")
    return lines


def get_file(tail_name):
    file = open(tail_name, 'r')
    lines = file.read()
    file.close()
    return lines

def print_lines(begin_line):
    global text
    global file_lines
    text.config(state=tk.NORMAL)
    split_lines = file_lines.split("\n")
    for num in range(begin_line, len(split_lines)-1):
        text.insert("end", (split_lines[num])+"\n")
        text.yview(tk.END)
     text.config(state=tk.DISABLED)
     text.update()

def update_window():
    try:
        global current_line
        global file_lines
        global pause_status
        if not pause_status:
            file_lines = get_file(argv[1])
            new_lines = get_current_line_count(file_lines)
            if new_lines > current_line:
                print_lines(current_line)
                current_line = new_lines
     except (KeyboardInterrupt, SystemExit):
         print("Now Exiting.....")
     root.after(1000, update_window)

def create_interface():
    global text
    global file_lines
    frame = tk.Frame(root, background="black")
    frame.place(x=10, y=10)
    frame2 = tk.Frame(root)
    scr = tk.Scrollbar(frame)
    text = tk.Text(frame, background="black", fg="green")
    text.insert("1.0", "Beginning of Tail File" + "\n")
    scr.config(command=text.yview)
    scr.pack(side="right", fill="y", expand=False)
    text.pack(side="left", fill="both", expand=True)
    frame.pack(side=tk.TOP, fill="both", expand=True)
    frame2.pack(side="bottom", anchor="w")
    pause = tk.Button(frame2, text="Pause", command=pause_updates)
    pause.pack()
    print_lines(0)
    update_window()


def correct_args(argv):
    if not len(argv) == 2:
        return False
    else:
        return True


if correct_args(argv):
    root = tk.Tk()
    root.title("Pytail v1.0 - Watching File")
    file_lines = get_file(argv[1])
    current_line = get_current_line_count(file_lines)
    create_interface()
    root.mainloop()

更改self.text_window = tkinter.Text(self.root, height=20, width=80)

self.text_window = tkinter.Label(self.root,textvariable=self.s, height=20, width=80)

然后在那之后擺脫這條線

那么您可以只做self.s.set("Test New Text!")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM