wxPython仅创建图像工具栏

2024-05-12 21:40:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个wxPython框架,它正好是我创建的工具栏的大小。你知道吗

我已经成功地创建了wx.框架空的工具栏,但窗口太大了,我如何使它正好适合工具栏。你知道吗

我试过的代码:

import wx

class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self)
        toolbar1.SetToolBitmapSize(wx.Size(64, 64))
        toolbar1.Realize()
        vbox.Add(toolbar1, 0, wx.EXPAND)
        self.SetSizer(vbox)
        self.SetTitle('Toolbars')
        self.Centre()
        vbox.Fit(self)

app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()

Tags: 代码importself框架appinitexamplewxpython
2条回答

我假设linux/mac上的工具栏大小是(0,0)的原因是b/c,没有附加工具。若要解决此问题,可以设置工具栏的最小大小,然后通过向工具栏大小添加框架标题和边框来计算框架大小。你知道吗

import wx


class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self)
        toolbar1.SetBackgroundColour(wx.RED)
        toolbar1.SetToolBitmapSize(wx.Size(64, 64))
        # prevent the sizer from setting the size to (0, 0) when there are no tools attached
        toolbar1.SetMinSize((64, 64))
        toolbar1.Realize()
        vbox.Add(toolbar1, 0, wx.EXPAND)
        self.SetSizer(vbox)
        self.SetTitle('Toolbars')
        self.Centre()
        vbox.Fit(self)
        self.Layout()

        # calculate the frame caption height
        caption_height = wx.SystemSettings.GetMetric(wx.SYS_CAPTION_Y, self)
        # *2 for the left and right border
        border_width = wx.SystemSettings.GetMetric(wx.SYS_BORDER_X, self) * 2

        # get the size of the toolbar
        sx, sy = toolbar1.GetSize()
        # set the frame size by adding the toolbar size to the border/caption size
        self.SetSize((sx + border_width, sy + caption_height))


app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()

无法将框架的大小设置为工具栏的大小。 差不多吧。你知道吗

self.size = toolbar1.size

相关问题 更多 >