Changeset 5638 for pjproject/trunk/pjsip-apps/src/pygui/log.py
- Timestamp:
- Aug 2, 2017 9:45:09 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjsip-apps/src/pygui/log.py
r4704 r5638 21 21 import sys 22 22 if sys.version_info[0] >= 3: # Python 3 23 24 25 23 import tkinter as tk 24 from tkinter import ttk 25 from tkinter import messagebox as msgbox 26 26 else: 27 28 29 27 import Tkinter as tk 28 import tkMessageBox as msgbox 29 import ttk 30 30 31 31 import pjsua2 as pj 32 32 import application 33 33 34 write=sys.stdout.write 34 35 35 36 class LogWindow(tk.Toplevel): 36 37 38 39 40 41 42 43 44 45 46 47 37 """ 38 Log window 39 """ 40 instance = None 41 def __init__(self, app): 42 tk.Toplevel.__init__(self, name='logwnd', width=640, height=480) 43 LogWindow.instance = self 44 self.app = app 45 self.state('withdrawn') 46 self.title('Log') 47 self._createWidgets() 48 self.protocol("WM_DELETE_WINDOW", self._onHide) 48 49 49 def addLog(self, entry): 50 """entry fields: 51 int level; 52 string msg; 53 long threadId; 54 string threadName; 55 """ 56 self.addLog2(entry.level, entry.msg) 57 58 def addLog2(self, level, msg): 59 if level==5: 60 tags = ('trace',) 61 elif level==3: 62 tags = ('info',) 63 elif level==2: 64 tags = ('warning',) 65 elif level<=1: 66 tags = ('error',) 67 else: 68 tags = None 69 self.text.insert(tk.END, msg, tags) 70 self.text.see(tk.END) 71 72 def _createWidgets(self): 73 self.rowconfigure(0, weight=1) 74 self.rowconfigure(1, weight=0) 75 self.columnconfigure(0, weight=1) 76 self.columnconfigure(1, weight=0) 77 78 self.text = tk.Text(self, font=('Courier New', '8'), wrap=tk.NONE, undo=False, padx=4, pady=5) 79 self.text.grid(row=0, column=0, sticky='nswe', padx=5, pady=5) 80 81 scrl = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.text.yview) 82 self.text.config(yscrollcommand=scrl.set) 83 scrl.grid(row=0, column=1, sticky='nsw', padx=5, pady=5) 50 def addLog(self, entry): 51 """entry fields: 52 int level; 53 string msg; 54 long threadId; 55 string threadName; 56 """ 57 self.addLog2(entry.level, entry.msg) 84 58 85 scrl = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.text.xview) 86 self.text.config(xscrollcommand=scrl.set) 87 scrl.grid(row=1, column=0, sticky='we', padx=5, pady=5) 88 89 self.text.bind("<Key>", self._onKey) 90 91 self.text.tag_configure('normal', font=('Courier New', '8'), foreground='black') 92 self.text.tag_configure('trace', font=('Courier New', '8'), foreground='#777777') 93 self.text.tag_configure('info', font=('Courier New', '8', 'bold'), foreground='black') 94 self.text.tag_configure('warning', font=('Courier New', '8', 'bold'), foreground='cyan') 95 self.text.tag_configure('error', font=('Courier New', '8', 'bold'), foreground='red') 96 97 def _onKey(self, event): 98 # Ignore key event to make text widget read-only 99 return "break" 100 101 def _onHide(self): 102 # Hide when close ('x') button is clicked 103 self.withdraw() 104 self.app.showLogWindow.set(0) 105 106 59 def addLog2(self, level, msg): 60 if level==5: 61 tags = ('trace',) 62 elif level==3: 63 tags = ('info',) 64 elif level==2: 65 tags = ('warning',) 66 elif level<=1: 67 tags = ('error',) 68 else: 69 tags = None 70 self.text.insert(tk.END, msg, tags) 71 self.text.see(tk.END) 72 73 def _createWidgets(self): 74 self.rowconfigure(0, weight=1) 75 self.rowconfigure(1, weight=0) 76 self.columnconfigure(0, weight=1) 77 self.columnconfigure(1, weight=0) 78 79 self.text = tk.Text(self, font=('Courier New', '8'), wrap=tk.NONE, undo=False, padx=4, pady=5) 80 self.text.grid(row=0, column=0, sticky='nswe', padx=5, pady=5) 81 82 scrl = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.text.yview) 83 self.text.config(yscrollcommand=scrl.set) 84 scrl.grid(row=0, column=1, sticky='nsw', padx=5, pady=5) 85 86 scrl = ttk.Scrollbar(self, orient=tk.HORIZONTAL, command=self.text.xview) 87 self.text.config(xscrollcommand=scrl.set) 88 scrl.grid(row=1, column=0, sticky='we', padx=5, pady=5) 89 90 self.text.bind("<Key>", self._onKey) 91 92 self.text.tag_configure('normal', font=('Courier New', '8'), foreground='black') 93 self.text.tag_configure('trace', font=('Courier New', '8'), foreground='#777777') 94 self.text.tag_configure('info', font=('Courier New', '8', 'bold'), foreground='black') 95 self.text.tag_configure('warning', font=('Courier New', '8', 'bold'), foreground='cyan') 96 self.text.tag_configure('error', font=('Courier New', '8', 'bold'), foreground='red') 97 98 def _onKey(self, event): 99 # Ignore key event to make text widget read-only 100 return "break" 101 102 def _onHide(self): 103 # Hide when close ('x') button is clicked 104 self.withdraw() 105 self.app.showLogWindow.set(0) 106 107 107 108 def writeLog2(level, msg): 108 109 LogWindow.instance.addLog2(level, msg) 109 if LogWindow.instance: 110 LogWindow.instance.addLog2(level, msg) 110 111 111 112 def writeLog(entry): 112 113 113 if LogWindow.instance: 114 LogWindow.instance.addLog(entry) 114 115 115 116 class Logger(pj.LogWriter): 116 117 118 119 120 121 122 123 print entry.msg, 124 117 """ 118 Logger to receive log messages from pjsua2 119 """ 120 def __init__(self): 121 pj.LogWriter.__init__(self) 122 123 def write(self, entry): 124 write(entry.msg + "\r\n") 125 writeLog(entry) 125 126 126 127 if __name__ == '__main__': 127 128 application.main()
Note: See TracChangeset
for help on using the changeset viewer.