diff options
author | Vikas Kushwaha <dev@vikas.rocks> | 2024-11-21 13:23:10 +0530 |
---|---|---|
committer | Vikas Kushwaha <dev@vikas.rocks> | 2024-11-21 13:23:10 +0530 |
commit | 8f0751170385989677392f806762a211f99412ef (patch) | |
tree | f09ad917798fa313edb77925a1ddbc6de8fb37ce /test/logs.py |
First Commit
Diffstat (limited to 'test/logs.py')
-rw-r--r-- | test/logs.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/test/logs.py b/test/logs.py new file mode 100644 index 0000000..6e602be --- /dev/null +++ b/test/logs.py @@ -0,0 +1,63 @@ +#!/bin/python2 + +import Tkinter +import logging +import datetime + +# this item "module_logger" is visible only in this module, +# (but you can create references to the same logger object from other modules +# by calling getLogger with an argument equal to the name of this module) +# this way, you can share or isolate loggers as desired across modules and across threads +# ...so it is module-level logging and it takes the name of this module (by using __name__) +# recommended per https://docs.python.org/2/library/logging.html +module_logger = logging.getLogger(__name__) + +class simpleapp_tk(Tkinter.Tk): + def __init__(self,parent): + Tkinter.Tk.__init__(self,parent) + self.parent = parent + + self.grid() + + self.mybutton = Tkinter.Button(self, text="ClickMe") + self.mybutton.grid(column=0,row=0,sticky='EW') + self.mybutton.bind("<ButtonRelease-1>", self.button_callback) + + self.mytext = Tkinter.Text(self, state="disabled") + self.mytext.grid(column=0, row=1) + + def button_callback(self, event): + now = datetime.datetime.now() + module_logger.info(now) + +class MyHandlerText(logging.StreamHandler): + def __init__(self, textctrl): + logging.StreamHandler.__init__(self) # initialize parent + self.textctrl = textctrl + + def emit(self, record): + msg = self.format(record) + self.textctrl.config(state="normal") + self.textctrl.insert("end", msg + "\n") + self.flush() + self.textctrl.config(state="disabled") + +if __name__ == "__main__": + + # create Tk object instance + app = simpleapp_tk(None) + app.title('my application') + + # setup logging handlers using the Tk instance created above + # the pattern below can be used in other threads... + # ...to allow other thread to send msgs to the gui + # in this example, we set up two handlers just for demonstration (you could add a fileHandler, etc) + stderrHandler = logging.StreamHandler() # no arguments => stderr + module_logger.addHandler(stderrHandler) + guiHandler = MyHandlerText(app.mytext) + module_logger.addHandler(guiHandler) + module_logger.setLevel(logging.INFO) + module_logger.info("from main") + + # start Tk + app.mainloop() |