python & tkinter:我正在使用的文本小部件不"pack"



我有一个问题,为什么我的文本 windget 不能正确打包在这段代码中。对于记录,它应该读取用户文档文件夹中的日志文件,并在"文本"小部件中显示内容,为红色警告和错误以及蓝色信息着色。任何帮助将不胜感激!

这是我想出的新代码,但文本小部件没有打包在主框架中......

#!/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()

工作完整的代码在这里,感谢所有帮助的人!

#!/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(self)
        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 10", width=200, height=60)
        self.output.tag_config("error", foreground="#FF0000")
        self.output.tag_config("warning", foreground="#00FF00")
        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
                self.output.config(state=tk.NORMAL)
                if "err" in tmp or "error" in tmp and not "overlayeffect" in tmp and not "animation" in tmp: #Error filter to display
                    self.output.insert(tk.END, tmp, "error")
                elif "lua" in tmp:
                    self.output.insert(tk.END, tmp, "info")
                elif "warn" in tmp:
                    self.output.insert(tk.END, tmp, "warning")
                self.oldline = tmp
            self.output.see(tk.END)
            self.update_idletasks()
            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.pack()
    gui.mainloop()

最新更新