wxPython工具栏帮助

wxPython工具栏帮助,python,user-interface,wxpython,toolbars,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_HOR

我是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()
我正试图清理一下代码,我希望工具栏有自己的类,所以当我想创建工具栏时,我简单地称它为这样的东西:

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()
我的问题是我怎样才能重写它,使它像那样工作?当前我的代码如下所示:

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函数?我怎么修理它?非常感谢您的帮助。谢谢

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

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

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()

您将获得所有好处。

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

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

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.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代码分解为如下对象。如果您打算手工编写GUI代码,那么维护起来就容易多了(我是这么做的)

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

然后在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)

另一件需要注意的事情是,通常情况下,导航和导航都要容易得多