Skip to content

Instantly share code, notes, and snippets.

@yuyosy
Last active January 28, 2021 17:12
Show Gist options
  • Save yuyosy/e5966ac99e443687b94ad60b7e14ee0b to your computer and use it in GitHub Desktop.
Save yuyosy/e5966ac99e443687b94ad60b7e14ee0b to your computer and use it in GitHub Desktop.
import wx
import ctypes
import logging
import wx
class FileDropToObject(wx.FileDropTarget):
def __init__(self, target):
wx.FileDropTarget.__init__(self)
self.target = target
def OnDropFiles(self, x, y, filenames):
logger = logging.getLogger('app')
for file in filenames:
logger.info('FileName:' + str(file))
self.target.SetValue(file)
return True
class TextFieldPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent, wx.ID_ANY)
label1 = wx.StaticText(self, wx.ID_ANY, "File name1:")
field1 = wx.TextCtrl(self, wx.ID_ANY, "", size=(400, -1))
field1.SetDropTarget(FileDropToObject(field1))
text_layout1 = wx.BoxSizer(wx.HORIZONTAL)
text_layout1.Add(label1, 0, wx.ALL, 5)
text_layout1.Add(field1, 1, wx.ALL | wx.EXPAND, 5)
label2 = wx.StaticText(self, wx.ID_ANY, "File name2:")
field2 = wx.TextCtrl(self, wx.ID_ANY, "", size=(400, -1))
field2.SetDropTarget(FileDropToObject(field2))
text_layout2 = wx.BoxSizer(wx.HORIZONTAL)
text_layout2.Add(label2, 0, wx.ALL, 5)
text_layout2.Add(field2, 1, wx.ALL | wx.EXPAND, 5)
layout = wx.BoxSizer(wx.VERTICAL)
layout.Add(text_layout1, 0, wx.ALL | wx.EXPAND, 5)
layout.Add(text_layout2, 0, wx.ALL | wx.EXPAND, 5)
self.SetSizer(layout)
def test_func():
logger = logging.getLogger('app')
for i in range(1, 5):
logger.info('test : ' + str(i))
# Logging Handler
class WxTextCtrlHandler(logging.Handler):
def __init__(self, ctrl):
logging.Handler.__init__(self)
self.ctrl = ctrl
def emit(self, record):
s = self.format(record) + '\n'
wx.CallAfter(self.ctrl.WriteText, s)
class LoggingPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent, wx.ID_ANY)
log = wx.TextCtrl(self, wx.ID_ANY, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
btn = wx.Button(self, wx.ID_ANY, 'Test loggging')
self.Bind(wx.EVT_BUTTON, self.onButton, btn)
layout = wx.BoxSizer(wx.VERTICAL)
layout.Add(log, 1, wx.ALL | wx.EXPAND, 5)
layout.Add(btn, 0, wx.ALL | wx.CENTER, 5)
self.SetSizer(layout)
logger = logging.getLogger('app')
handler = WxTextCtrlHandler(log)
logger.addHandler(handler)
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(message)s"))
logger.setLevel(logging.DEBUG)
logger.info('Setup logger in MainFrame')
def onButton(self, event):
logger = logging.getLogger('app')
logger.info('Start Test')
test_func()
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title="ApplicationTitle", size=(600, 340))
self.__set_layout()
def __set_layout(self):
textfield_panel = TextFieldPanel(self)
logging_panel = LoggingPanel(self)
root_layout = wx.BoxSizer(wx.VERTICAL)
root_layout.Add(textfield_panel, 0, wx.ALL | wx.EXPAND)
root_layout.Add(logging_panel, 1, wx.ALL | wx.EXPAND)
self.SetSizer(root_layout)
# Application Object
class Application(wx.App):
def OnInit(self):
frame = MainFrame()
self.SetTopWindow(frame)
frame.Show(True)
return True
if __name__ == '__main__':
try:
# High-DPI Settings
ctypes.windll.shcore.SetProcessDpiAwareness(True)
except:
pass
app = Application()
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment