Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python&;tkinter:文本小部件I';m使用不';";“包装”;_Python_Tkinter_Tk_Text Widget - Fatal编程技术网

python&;tkinter:文本小部件I';m使用不';";“包装”;

python&;tkinter:文本小部件I';m使用不';";“包装”;,python,tkinter,tk,text-widget,Python,Tkinter,Tk,Text Widget,我有一个问题,为什么我的文本windget在这段代码中没有正确打包。对于记录,它应该读取users Documents文件夹中的日志文件,并在文本小部件中显示内容,将红色警告、错误和蓝色信息着色。任何帮助都将不胜感激 这是我提出的新代码,但是文本小部件没有打包在主框架中 #!/usr/bin/python3 import tkinter as tk import sys import time from os.path import expanduser def get_log_path():

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

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

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

工作完整的代码在这里,感谢大家的帮助

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

你能告诉我们错误信息吗?或者没有?你能更具体地说明什么是不正确打包吗?也许我错了,但是self.output.pack()不应该在self.output.config()之后吗?@Cribber:pack和
config
的顺序是不相关的。你还有一个问题,那就是你正在创建一个内部框架(
self.frame
)作为根目录的子目录,而不是
GUI
的子目录。虽然这会起作用,但这不是你应该怎么做。使
GUI
tk.Frame
继承的全部目的是使该类创建的所有内容都在该框架内。您能给我们错误消息吗?或者没有?你能更具体地说明什么是不正确打包吗?也许我错了,但是self.output.pack()不应该在self.output.config()之后吗?@Cribber:pack和
config
的顺序是不相关的。你还有一个问题,那就是你正在创建一个内部框架(
self.frame
)作为根目录的子目录,而不是
GUI
的子目录。虽然这会起作用,但这不是你应该怎么做。使
GUI
tk.Frame
继承的全部要点是,该类创建的所有内容都在该框架内。您忽略了将
master
传递给`super(GUI,self)。\uuu init\uuuu()
。在这种特定的情况下,它是无害的,因为没有任何一个master与拥有
root`作为master是一样的,但一般来说,您应该养成将它传递给master的习惯。您忽略了将
master
传递给`super(GUI,self)。\uu init\uuuu()
。在这种特定的情况下,它是无害的,因为没有任何master与拥有
root`作为master是一样的,但是一般来说,您应该养成传递它的习惯。