繁体   English   中英

wxPython工具栏帮助

[英]wxPython toolbar help

我是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函数吗? 我如何解决它? 任何帮助是极大的赞赏。 谢谢

可以使用函数代替设置工具栏的类。 该函数可以是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()

您将获得所有好处。

是。 将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样式指南

要注意的另一件事是, wxWidgets文档通常更易于浏览和解密

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM