使用wxPython更改工具栏中的标签

时间:2010-11-30 15:53:25

标签: python wxpython wxwidgets

我目前在wxpython中有一个带有开始图标的工具栏。我想要它,所以当点击这个图标时,它使用的图标和方法会改变以停止。

这是我到目前为止的代码:

#!/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 startLiveUpdate(self, event):
      pass  


if __name__ == '__main__':
    app = wx.App(False)
    logClient(None, -1, "Log Event Viewer")
    app.MainLoop()

不确定要在startLiveUpdate方法中添加什么内容?

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

这是一个很快被黑客攻击的人。在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()

答案 1 :(得分:0)

与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()
相关问题