wxPython 2.9升级后的工具栏渲染

时间:2011-11-28 20:40:02

标签: macos wxpython toolbar

此代码在wxPython 2.8上运行良好,今天升级到2.9但是工具栏没有 完全显示。如果我删除self.SetToolBar()调用,图标确实显示但不显示为按钮,并且在重新调整屏幕大小时工具栏格式不会延伸。有任何想法吗?

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = Example(None, title="Word Bag", size=(400,100))
        self.SetTopWindow(self.frame)
        self.frame.Show()

        return True


class MyToolbar(wx.ToolBar):
    """Toolbars are attached to frames, so need TBar = Toolbar(self) in frame init"""
    def __init__(self, parent):
        wx.ToolBar.__init__(self, parent)

        # set my preferred default size for icons
        self.SetToolBitmapSize((32,32))

        # the main bit where icons are formatted, added, and bound to handlers
        self.initialiseIcons()

        # Need to call realise before exiting
        self.Realize()

    def initialiseIcons(self):
        """Iterate over icons and add them to toolbar"""
        for each in self.toolbarData():
            self.createSimpleTool(*each)

    def createSimpleTool(self, label, filename, statbar, handler):
        """Adds icons to bar using AddSimpleTool"""
        if not label:
            self.AddSeparator()
            return
        bmp = wx.Image(filename, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
        tool = self.AddSimpleTool(-1, bmp, label, statbar)
        self.Bind(wx.EVT_MENU, handler, tool)

    def toolbarData(self):
        """Put your icon data here in the following format...
        [0] = tooltip label, [1] = bitmap path, [2] = status bar label, [3] = bound function"""
        return [["Add new word","/Users/paulpatterson/Desktop/add.png","Add a new word to the dictionary",self.OnAddWord]]

    # toolbar icon handlers here...
    def OnAddWord(self, event):
        pass

    def OnRemoveWord(self, event):
        pass

    def OnSearchWord(self, event):
        pass



class Example(wx.Frame):

    def __init__(self, parent, title, size): 
        super(Example, self).__init__(parent, title=title, size=size)

        # Create and set the toolbar
        tBar = MyToolbar(self)
        self.SetToolBar(tBar)

        self.frameSizer = wx.BoxSizer(wx.VERTICAL)

        self.panelOne = MyPanel(self)



        self.frameSizer.Add(self.panelOne, 1, wx.EXPAND)
        self.SetSizer(self.frameSizer)
        #self.frameSizer.Fit(self)

        self.Centre() 
        self.Show()


class MyPanel(wx.Panel):

    def __init__(self, parent):
        super(MyPanel, self).__init__(parent)

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)


        ### widgets here


        # set optimum layout for mainsizer...
        self.SetSizer(self.mainSizer)
        # ...then fit main sizer to the panel.
        self.mainSizer.Fit(self)


if __name__ == '__main__':

    app = MyApp(False)
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

几小时前我遇到了同样的问题。我无法使用自定义创建的工具栏,但使用Frame.CreateToolBar()按预期工作。例如:

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(
            self, parent=None, id=-1, title='Test window',
            size=wx.Size(800, 600)
        )

        self.setup_toolbar()

    def setup_toolbar(self):
        # First create the toolbar.
        self.toolbar = self.CreateToolBar(wx.TB_FLAT | wx.TB_TEXT)
        self.Bind(wx.EVT_TOOL, self.on_toolbar)

        # Add a 'Clear all' button.
        self.toolbar.AddLabelTool(
            wx.ID_NEW, 'Clear all', get_toolbar_art('new_big'),
            shortHelp='Remove all the contents from the text inputs.'
        )

        # Add an 'Open' button.
        self.toolbar.AddLabelTool(
            wx.ID_OPEN, 'From file...', get_toolbar_art('open_big'),
            shortHelp='Fill the input box with the ' +
                      'contents of a Linjekort text file.'
        )

        # self.toolbar.AddSeparator()   # A separator.

        # Add a 'Save all' button.
        self.toolbar.AddLabelTool(
            wx.ID_SAVE, 'Save results to...', get_toolbar_art('save_big'),
            shortHelp='Save all the Ozi files to a directory.'
        )

        self.toolbar.Realize()

def get_toolbar_art(name):
    return wx.Bitmap('icons/{}.png'.format(name))

但这并没有回答如何让自定义工具栏子类工作。您是否尝试使用sizer将工具栏添加到布局中,而不是使用SetToolBar函数?这是我所知道的唯一方法,以避免最终使用OSX本机Frame工具栏。以下是完成此操作的示例:

def create_output_panel(self, parent, slug, label):
    panel = wx.Panel(parent, style=wx.BORDER_THEME)
    panel.SetBackgroundColour(
        wx.SystemSettings.GetColour(wx.SYS_COLOUR_3DSHADOW)
    )

    # Toolbar.
    toolbar = wx.ToolBar(panel, -1, style=wx.BORDER_RAISED)
    toolbar.AddSimpleTool(
        wx.ID_SAVE, get_toolbar_art('save'),
        shortHelpString='Save to file...'
    )
    toolbar.Realize()
    toolbar.Bind(wx.EVT_TOOL, lambda evt: print('Success'))

    # Text control.
    textctrl = wx.TextCtrl(
        panel, -1, style=wx.TE_READONLY | wx.TE_MULTILINE | wx.BORDER_NONE
    )

    # Organize controls.
    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(
        wx.StaticText(panel, -1, label), proportion=0, border=5, flag=wx.ALL
    )
    sizer.Add(toolbar, proportion=0, flag=wx.EXPAND)
    sizer.Add(textctrl, proportion=1, flag=wx.EXPAND)

    panel.SetSizer(sizer)
    return panel

屏幕截图:

enter image description here

我希望这有帮助!