Skip to content

Instantly share code, notes, and snippets.

@aguegu
Last active December 28, 2015 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aguegu/7470227 to your computer and use it in GitHub Desktop.
Save aguegu/7470227 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import wx
import os
class MyFrame(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(200, 100))
self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)
self.CreateStatusBar()
fileMenu = wx.Menu()
menuOpen = fileMenu.Append(wx.ID_OPEN, "&Open", "oepn a file")
menuAbout = fileMenu.Append(wx.ID_ABOUT, "&About", "Information about this program")
fileMenu.AppendSeparator()
menuExit = fileMenu.Append(wx.ID_EXIT, "E&xit", "Terminate this program")
menuBar = wx.MenuBar()
menuBar.Append(fileMenu, "&File")
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
self.Show(True)
def OnAbout(self, e):
dlg = wx.MessageDialog(self, "A small text editor", "About")
dlg.ShowModal()
dlg.Destroy()
def OnExit(self, e):
self.Close(True)
def OnOpen(self,e):
dlg = wx.FileDialog(self, "Choose a file", ".", "", "*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
f = open(dlg.GetPath(), 'r')
self.control.SetValue(f.read())
f.close()
dlg.Destroy()
app = wx.App(False)
frame = MyFrame(None, "Small editor")
app.MainLoop()
#!/usr/bin/env python
import wx
import os
import sys
class RedirectTextCtrl(object):
def __init__(self, txt):
self.out = txt
def write(self, string):
self.out.WriteText(string)
class ElkUI(wx.Frame):
def __init__(self, parent, title):
super(ElkUI, self).__init__(parent, wx.ID_ANY, title, size=(600, 400))
self.Init()
self.Centre()
self.Show()
self.redirect()
def Init(self):
panel = wx.Panel(self)
self.txtLog = wx.TextCtrl(panel, wx.ID_ANY, style=wx.TE_MULTILINE|wx.HSCROLL)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.txtLog, 1, wx.ALL|wx.EXPAND, 2)
btnChoose = wx.Button(panel, wx.ID_ANY, "Choose")
self.txtPath = wx.TextCtrl(panel, wx.ID_ANY)
btnVerify = wx.Button(panel, wx.ID_ANY, "Verify")
btnUpdate = wx.Button(panel, wx.ID_ANY, "Update")
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(btnChoose, 0, wx.ALL, 0)
hbox.Add(self.txtPath, 1, wx.EXPAND | wx.ALL, 0)
hbox.Add(btnVerify, 0, wx.ALL, 0)
hbox.Add(btnUpdate, 0, wx.ALL, 0)
vbox.Add(hbox, 0, wx.ALL|wx.EXPAND, 2)
panel.SetSizer(vbox)
self.Bind(wx.EVT_BUTTON, self.onChoose, btnChoose)
self.Bind(wx.EVT_BUTTON, self.onVerify, btnVerify)
def redirect(self):
sys.stdout = RedirectTextCtrl(self.txtLog)
def onChoose(self, e):
dlg = wx.DirDialog(self, 'choose the folder contains excel files', '.')
if dlg.ShowModal() == wx.ID_OK:
self.txtPath.Clear()
self.txtPath.WriteText(dlg.GetPath())
dlg.Destroy()
def onVerify(self, e):
for root, dirs, files in os.walk(self.txtPath.GetValue()):
for file in files:
if file.endswith('.mkv') and not file.lower().endswith('sample.mkv'):
print("%s" % os.path.join(root, file))
if __name__ == '__main__':
app = wx.App()
ElkUI(None, "elk")
app.MainLoop()
import time
from threading import *
import wx
EVENT_TYPE_ID = wx.NewId()
class WorkerThread(Thread):
def __init__(self, notify_window):
Thread.__init__(self)
self._notify_window = notify_window
self._want_abort = 0
self.start()
self.event = wx.PyEvent(eventType=EVENT_TYPE_ID)
def run(self):
for i in range(10):
time.sleep(1)
self.post(i)
if self._want_abort:
self.post(None)
return
self.post(10)
def abort(self):
self._want_abort = 1
def post(self, data):
self.event.data = data
wx.PostEvent(self._notify_window, self.event)
class MainFrame(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, 'Thread Test')
btn_start = wx.Button(self, wx.ID_ANY, 'Start', pos=(0,0))
btn_stop = wx.Button(self, wx.ID_ANY, 'Stop', pos=(0,50))
self.status = wx.StaticText(self, -1, '', pos=(0,100))
self.Bind(wx.EVT_BUTTON, self.OnStart, btn_start)
self.Bind(wx.EVT_BUTTON, self.OnStop, btn_stop)
self.Connect(-1, -1, EVENT_TYPE_ID, self.OnResult)
self.worker = None
def OnStart(self, event):
if not self.worker:
self.status.SetLabel('Starting computation')
self.worker = WorkerThread(self)
def OnStop(self, event):
if self.worker:
self.status.SetLabel('Trying to abort computation')
self.worker.abort()
def OnResult(self, event):
if event.data is None:
self.status.SetLabel('Computation aborted')
else:
self.status.SetLabel('Computation Result: %s' % event.data)
self.worker = None
class MainApp(wx.App):
def OnInit(self):
self.frame = MainFrame(None, -1)
self.frame.Show(True)
self.SetTopWindow(self.frame)
return True
if __name__ == '__main__':
app = MainApp(0)
app.MainLoop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment