2017-04-12 5 views
1

왜이 텍스트 부분에 내 텍스트 윈젯이 제대로 포장되지 않는지에 대한 질문이 있습니다. 기록의 경우 사용자 문서 폴더의 로그 파일을 읽고 텍스트 위젯에 내용을 표시하여 빨간색 경고 및 오류 및 파란색 정보를 표시합니다. 어떤 도움이라도 대단히 감사하겠습니다!파이썬 & tkinter : 내가 사용하는 텍스트 위젯이 "포장하지 않습니다"

여기에 내가 생각 해낸 새로운 코드는하지만 텍스트 위젯은 메인 프레임 안에 포장하지 않습니다 ...

#!/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 [email protected] 
       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 [email protected] 
        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() 
+0

오류 메시지를 보내 주시겠습니까? 아니면 아무도 없습니까? – Cribber

+0

당신은 더 이상 구체적으로 무엇을 의미 할 수 있습니까? – WhatsThePoint

+0

어쩌면 내가 틀렸어.하지만 self.output.pack()이 self.output.config() 다음에 오면 안된다? – Cribber

답변

0

은 작업 완료 코드는 도움을 주신 모든 분들께 감사드립니다!

#!/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 [email protected] 
       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() 
+0

당신은'master (을)를'super (GUI, self) .__ init __()'에 전달하는 것을 무시하고 있습니다. 마스터가'루트 '를 마스터로 갖는 것과 같지 않기 때문에이 특별한 경우에는 무해합니다. 그러나 일반적으로 마스터를 전달하는 습관을 갖춰야합니다. –