概述
所以我把这一切都搞错了。因为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()
最后
以上就是唠叨灰狼为你收集整理的python 窗口更新_pythontkinter更新文本窗口的全部内容,希望文章能够帮你解决python 窗口更新_pythontkinter更新文本窗口所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复