wxPython 工具栏帮助

发布于 2024-07-14 11:08:56 字数 1219 浏览 11 评论 0原文

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

我不太确定“self”是如何工作的。 我需要重写init函数吗? 我如何解决它? 任何帮助是极大的赞赏。 谢谢

I am new to Python. I am writing an application using wxPython and I currently my code that generates a toolbar looks like this:

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

I am trying to clean up the code a bit and I want the toolbar to have its own class so when I want to create a toolbar, I simply call it something like this:

toolbar = Toolbar()

My question is how can I rewrite it so it works like that? Currently my code looks like this:

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

I am not quite sure how 'self' works. Do I need to rewrite the init function? How do I fix it? Any help is greatly appreciated. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你是孤单的心事 2024-07-21 11:08:56

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

您将获得所有好处。

Instead of a class that sets up your toolbar, use a function. The function can be a member function of your Window that subclasses wx.Frame. That way, the toolbar will get Created from the correct window, and be attached the way you would expect.

The class that you're writing above would work, if it knew which wx.Frame (your class called Window) to connect the toolbar to. To get it to work you would have to pass the frame object to the toolbar creator class...

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

It looks like a quick fix... but really using a class to do this is not a good use of classes. (I'd even go so far as to say it was incorrect.)

Really, what would clean things up a bit would be just to move the toolbar stuff to its own member function:

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

You get all the benefits.

疏忽 2024-07-21 11:08:56

是的。 将你的 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

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

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 文档 非常多更容易导航和破译

Yes. Break your wxpython code into objects like this. It is much easier to maintain if you are going to code your GUI by hand (I do).

You need to subclass wx.ToolBar (which itself is a subclass of wx.ToolBarBase, and most of wx.ToolBar's functions are derived from that namespace):

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

Then in your __init__ for your wx.Frame call your toolbar:

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

Another thing to note is that often the wxWidgets docs are much easier to navigate and to decipher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文