所以我是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")
答案 0 :(得分:2)
所以我认为这一切都错了。因为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()
答案 1 :(得分:-1)
更改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!")