wxPython工具栏帮助

时间:2009-02-27 19:13:09

标签: python user-interface wxpython toolbars

我是Python新手。我正在使用wxPython编写应用程序,目前我生成工具栏的代码如下所示:

class Window(wx.Frame)
def __init__(self, parent, plot):
    wx.Frame.__init__(self, parent, wx.ID_ANY, "Name", size =(900, 600))
    self.Centre()

    self.toolbar = self.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
    self.toolbar.SetToolBitmapSize((32,32))
    self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
    self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
    self.toolbar.AddSeparator()
    self.toolbar.Realize()

我正在尝试清理代码,我希望工具栏有自己的类,所以当我想创建一个工具栏时,我简单地称之为:

toolbar = Toolbar()

我的问题是我怎样才能重写它以便这样工作呢?目前我的代码如下所示:

class Toolbar():
    def __init__(self):
        self.toolbar = self.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
        self.toolbar.SetToolBitmapSize((32,32))
        self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
        self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
        self.toolbar.AddSeparator()
        self.toolbar.Realize()

我不太确定“自我”的运作方式。我是否需要重写 init 功能?我如何解决它?任何帮助是极大的赞赏。感谢

2 个答案:

答案 0 :(得分:3)

使用函数代替设置工具栏的类。该函数可以是Window的成员函数,它是wx.Frame的子类。这样,工具栏将从正确的窗口中创建,并按照您期望的方式连接。

如果您知道将工具栏连接到哪个wx.Frame(您的类称为Window),那么您上面写的类就可以工作了。要使其工作,您必须将框架对象传递给工具栏创建者类...

class Toolbar():
  def __init__(self, frame_to_connect_to):
    frame_to_connect_to.toolbar = frame_to_connect_to.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
    frame_to_connect_to.toolbar.SetToolBitmapSize((32,32))
    frame_to_connect_to.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
    frame_to_connect_to.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
    frame_to_connect_to.toolbar.AddSeparator()
    frame_to_connect_to.toolbar.Realize()

它看起来像是一个快速解决方案......但真正使用类来完成这项工作并不是很好用。 (我甚至会说它不正确。)

真的,将一些事情清理干净只会将工具栏内容移动到自己的成员函数中:

class Window(wx.Frame)
  def __init__(self, parent, plot):
    wx.Frame.__init__(self, parent, wx.ID_ANY, "Name", size =(900, 600))
    self.Centre()
    self._init_toolbar()

  def _init_toolbar(self):
    self.toolbar = self.CreateToolBar(style=(wx.TB_HORZ_LAYOUT | wx.TB_TEXT))
    self.toolbar.SetToolBitmapSize((32,32))
    self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/fileopen.png'))
    self.toolbar.AddLabelTool(3, '', wx.Bitmap('GUI/icons/filesave.png'))
    self.toolbar.AddSeparator()
    self.toolbar.Realize()

你可以获得所有好处。

答案 1 :(得分:1)

是。将您的wxpython代码分解为这样的对象。如果要手动编写GUI代码(我这样做),维护起来要容易得多。

您需要子类wx.ToolBar(它本身是wx.ToolBarBase的子类,并且大多数wx.ToolBar的函数都是从该命名空间派生的):

class MyToolBar(wx.ToolBar):
    def __init__(self, parent, *args, **kwargs):
        wx.ToolBar.__init__(self, parent, *args, **kwargs)
        #note self here and not self.toolbar
        self.SetToolBitmapSize((32,32))
        #add other code here

然后在__init__中为你的wx.Frame调用你的工具栏:

class MyFrame(wx.Frame):
    def __init__(self, parent, *args, **kwargs):
        wx.Frame.__init__(self, parent, *args, **kwargs)
        #note that below, self refers to the wx.Frame
        #self(wx.Frame) = parent for the toolbar constructor
        toolbar = MyToolBar(self)

wxPython Style Guide

需要注意的另一点是wxWidgets docs通常更容易导航to decipher