Changing Label in toolbar using wxPython
I currently have a toolbar in wxpython with an start icon. I want it so when this icon is clicked the icon and method that it uses changes to stop.
This is the code that I have so far:
#!/usr/bin/env python
# encoding: utf-8
"""
logClient2.py
Created by Allister on 2010-11-30.
"""
import wx
import sqlite3
WINDOW_SIZE = (900,400)
class logClient(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=WINDOW_SIZE)
self.toolbar = self.CreateToolBar()
self.toolbar.AddLabelTool(1, 'Refresh', wx.Bitmap('icons/refresh_icon.png'))
self.toolbar.Realize()
self.Bind(wx.EVT_TOOL, self.startLiveUpdate, id=1)
self.Show(True)
def startLiveUpd开发者_运维问答ate(self, event):
pass
if __name__ == '__main__':
app = wx.App(False)
logClient(None, -1, "Log Event Viewer")
app.MainLoop()
Not really sure what to put in the startLiveUpdate method ?
Thanks for any help!
Here's a quickly hacked together one. Tested on Ubuntu 9.10, Python 2.6, wx 2.8.10.1
#!/usr/bin/env python
# encoding: utf-8
"""
logClient2.py
Created by Allister on 2010-11-30.
"""
import wx
import sqlite3
WINDOW_SIZE = (900,400)
class logClient(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=WINDOW_SIZE)
self.toolbar = self.CreateToolBar()
self.startLiveUpdate(None)
self.Show(True)
def startLiveUpdate(self, event):
self.createToolbarItem("Refresh", "refresh.jpg", self.stopLiveUpdate)
def stopLiveUpdate(self, event):
self.createToolbarItem("Stop", "refresh2.jpg", self.startLiveUpdate)
def createToolbarItem(self, label, imageName, method):
self.toolbar.RemoveTool(1)
self.toolbar.AddLabelTool(1, label, wx.Bitmap(imageName))
self.toolbar.Realize()
self.Bind(wx.EVT_TOOL, method, id=1)
if __name__ == '__main__':
app = wx.App(False)
logClient(None, -1, "Log Event Viewer")
app.MainLoop()
Here is a less hacked version than the accepted answer for wxpython 4.x
self.tool = self.toolbar.AddTool(-1, "A tool", "image.png")
...
def change_tool_label(self):
self.tool.SetLabel("A new label")
# need to call Realize() to re-draw the toolbar
self.toolbar.Realize()
精彩评论