【发布时间】:2017-09-08 05:36:25
【问题描述】:
我有一个问题,为什么我的文本windget 没有正确打包到这段代码中。作为记录,它应该读取用户文档文件夹中的日志文件,并在文本小部件中显示内容,将红色警告和错误以及蓝色信息着色。任何帮助将不胜感激!
这是我想出的新代码,但 Text 小部件没有打包在主框架内...
#!/usr/bin/python3
import tkinter as tk
import sys
import time
from os.path import expanduser
def get_log_path():
p = ""
if sys.platform == "linux" or sys.platform == "linux2":
p = expanduser("~/.local/share/binding of isaac afterbirth+/log.txt")
elif sys.platform == "darwin":
p = expanduser("~/Library/Application Support/Binding of Isaac Afterbirth+/log.txt")
elif sys.platform == "win32":
p = expanduser("~/Documents/My Games/binding of isaac afterbirth+/log.txt")
return p
pass
class GUI(tk.Frame):
def __init__(self, master=None):
super(GUI, self).__init__()
self.master = master
self.start_stop = False
self.log_path = get_log_path()
self.output = tk.Text(self)
self.menubar = tk.Menu(master)
self.menubar.add_command(label="Start", command=self.start)
self.menubar.add_command(label="Stop", command=self.stop)
master.config(menu=self.menubar)
self.oldline = " "
self.init_layout()
pass
def init_layout(self):
self.output.config(font="sans 12", width=200, height=60, state = tk.DISABLED)
self.output.tag_config("error", foreground="#FF0000")
self.output.tag_config("info", foreground="#0000FF")
self.output.pack()
self.readfile()
pass
def readfile(self):
if self.start_stop:
tmp = self.log_f.readline().lower()
if self.oldline != tmp: #display spam only once@FileLoad
if "err" in tmp or "error" in tmp or "warn" in tmp and not "overlayeffect" in tmp and not "animation" in tmp: #Error filter to display
#print(tmp, end='', file=sys.stderr)
self.output.insert(tk.END, tmp)
index = "end - 1 lines"
self.output.tag_add("error", index)
elif "lua" in tmp:
self.output.insert(tk.END, tmp)
index = "end - 1 lines"
self.output.tag_add("info", index)
self.oldline = tmp
self.after(5, self.readfile)
pass
pass
def start(self):
self.log_f = open(self.log_path, "r")
self.start_stop = True
self.readfile()
pass
def stop(self):
self.log_f.close()
self.start_stop = False
pass
pass
if __name__ == "__main__":
root = tk.Tk()
root.title("Isaac Debug Helper")
root.geometry("650x500")
gui = GUI(root)
gui.mainloop()
还有旧代码供参考:
#!/usr/bin/python3
import tkinter as tk
import sys
import time
from os.path import expanduser
def get_log_path():
p = ""
if sys.platform == "linux" or sys.platform == "linux2":
p = expanduser("~/.local/share/binding of isaac afterbirth+/log.txt")
elif sys.platform == "darwin":
p = expanduser("~/Library/Application Support/Binding of Isaac Afterbirth+/log.txt")
elif sys.platform == "win32":
p = expanduser("~/Documents/My Games/binding of isaac afterbirth+/log.txt")
return p
pass
class GUI(tk.Frame):
def __init__(self, master=None):
super(GUI, self).__init__()
self.master = master
self.start_stop = True
self.log_path = get_log_path()
self.output = tk.Text(self)
self.frame=tk.Frame()
# self.reloadButton = tk.Button(self.frame, text="Reload", command=self.reload)
self.startButton = tk.Button(self.frame, text="Start", command=self.start)
self.stopButton = tk.Button(self.frame, text="Stop", command=self.stop)
self.oldline = " "
self.init_layout()
pass
def init_layout(self):
self.output.pack(side=tk.LEFT)#, fill=tk.BOTH, expand=1)
self.output.config(font="sans 12", width=200, height=60, state = tk.DISABLED)
self.output.tag_config("error", foreground="#FF0000")
self.output.tag_config("info", foreground="#0000FF")
self.frame.pack(side=tk.RIGHT)
# self.reloadButton.pack(in_=self.frame)
self.startButton.pack(in_=self.frame)
self.stopButton.pack(in_=self.frame)
self.readfile()
pass
def readfile(self):
if self.start_stop:
with open(self.log_path, "r") as f:
tmp = f.readline().lower()
if self.oldline != tmp: #display spam only once@FileLoad
if "err" in tmp or "error" in tmp or "warn" in tmp and not "overlayeffect" in tmp and not "animation" in tmp: #Error filter to display
#print(tmp, end='', file=sys.stderr)
self.output.insert(tk.END, tmp)
index = "end - 1 lines"
self.output.tag_add("error", index)
elif "lua" in tmp:
self.output.insert(tk.END, tmp)
index = "end - 1 lines"
self.output.tag_add("info", index)
self.oldline = tmp
pass
self.after(5, self.readfile)
pass
pass
# def reload(self):
# pass
def start(self):
self.start_stop = True
self.readfile()
pass
def stop(self):
self.start_stop = False
pass
pass
if __name__ == "__main__":
root = tk.Tk()
root.title("Isaac Debug Helper")
root.geometry("650x500")
gui = GUI(root)
gui.mainloop()
【问题讨论】:
-
你能给我们错误信息吗?还是没有?
-
您能更具体地说明没有正确包装是什么意思吗?
-
也许我错了,但 self.output.pack() 不应该出现在 self.output.config() 之后吗?
-
@Cribber:
pack和config的顺序无关紧要。 -
您还有一个问题,即您正在创建一个内部框架 (
self.frame) 作为根的子级而不是GUI的子级。虽然这会起作用,但这不是你应该这样做的方式。使GUI继承自tk.Frame的全部意义在于,该类创建的所有内容都在该框架内。
标签: python tkinter tk text-widget