Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Stub of a simple wxpython clipboard manager
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
TASKBAR_ICON = "icon.png"
class ClipmanIcon(wx.TaskBarIcon):
def __init__(self):
wx.TaskBarIcon.__init__(self)
self.icon = wx.IconFromBitmap( wx.Bitmap(TASKBAR_ICON) )
self.UpdateIcon()
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.OnLMBDown)
self.Timer = wx.Timer(self)
self.Timer.Start(100)
self.Bind(wx.EVT_TIMER, self.OnTimer, self.Timer)
self.RecentHistory = [ ]
def CreatePopupMenu(self):
menu = wx.Menu()
item_exit = menu.Append(wx.ID_EXIT, 'Exit', 'Exit!')
menu.Bind(wx.EVT_MENU, self.OnExit, item_exit)
return menu
def OnTimer(self, event):
wx.TheClipboard.Open()
data_object = wx.TextDataObject()
success = wx.TheClipboard.GetData(data_object)
wx.TheClipboard.Close()
if not success: return None
text = data_object.Text
if text not in self.RecentHistory:
self.RecentHistory += [text]
elif text != self.RecentHistory[-1]:
index = self.RecentHistory.index(text)
del self.RecentHistory[ index ]
self.RecentHistory += [text]
def ChooseItem(self, index):
print index
def CreateHistoryMenu(self):
menu = wx.Menu()
for i, h_item in enumerate(self.RecentHistory):
if i == 0: item_id = wx.ID_FORWARD
else: item_id = wx.ID_ANY
item = menu.Append(item_id, h_item)
func = lambda e: self.ChooseItem(i)
menu.Bind(wx.EVT_MENU, func, item)
menu.AppendSeparator()
clear = menu.Append(wx.ID_CLEAR, "Clear")
menu.Bind(wx.EVT_MENU, self.ClearRecent, clear)
return menu
def ClearRecent(self, event):
self.RecentHistory = []
self.OnTimer(None)
def OnExit(self, event):
self.Timer.Destroy()
wx.CallAfter(self.Destroy)
def UpdateIcon(self):
self.SetIcon(self.icon, "Derpo derpo")
def OnLMBDown(self, event):
self.PopupMenu(self.CreateHistoryMenu())
class ClipmanApp(wx.App):
def __init__(self):
wx.App.__init__(self, False)
icon = ClipmanIcon()
try:
app = ClipmanApp()
app.MainLoop()
except KeyboardInterrupt:
app.Destroy()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment