【发布时间】:2014-12-08 21:56:13
【问题描述】:
所以我是 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.Text 中使用 textvariable