渐变条上的按钮可以访问不同的面板

时间:2014-01-20 22:18:44

标签: python button user-interface wxpython wxwidgets

以下代码生成一个带有灰色渐变条的窗口。

enter image description here

import wx

class GradientFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
        self.Bind(wx.EVT_SIZE, self.OnSize)
        self.Center()
        self.Show()

    def OnSize(self, event):
        event.Skip()
        self.Refresh()

    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self)
        rect = self.GetClientRect()
        dc.SetBackground(wx.Brush("white"))
        dc.Clear() 
        rect.SetHeight(30)
        dc.GradientFillLinear(rect, '#fbfbfb', '#efefef', wx.SOUTH)
        rect.SetTop(30)
        rect.SetHeight(2)
        dc.GradientFillLinear(rect, '#dbdbdb', '#c1c1c1', wx.SOUTH)

app = wx.App(0)
frame = GradientFrame(None, 'Test')
app.MainLoop()

我想添加像以下屏幕截图那样的按钮,允许访问GUI的不同页面 / 面板(每个都包含自己的小部件,等)

enter image description here

的优秀框架是什么:这些按钮是在OnPaint手动创建的(这可能是非常“低级”)还是其他地方?是否有现成的方式来使用链接到不同页面的按钮

1 个答案:

答案 0 :(得分:2)

没有用于创建自定义小部件的良好框架。但是,有一些很好的食谱:

这两个链接应该让你入门。您还可以查看GenericButton,AquaButton或PlateButton的来源以获取更多想法。

或者,您也可以创建一个特定尺寸的面板,然后将一些自定义按钮或常规按钮放在其上。

以下是一个如何使用PlateButtons的示例:

import wx
import wx.lib.platebtn as platebtn

class GradientFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title)
        self.panel = wx.Panel(self)
        self.panel.Bind(wx.EVT_PAINT, self.OnPaint)
        self.panel.Bind(wx.EVT_ERASE_BACKGROUND, lambda event: None)
        self.panel.Bind(wx.EVT_SIZE, self.OnSize)

        # add plate buttons
        top_sizer = wx.BoxSizer(wx.VERTICAL)
        btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
        labels = ["Morceaux", "Albums", "Artistes", "Genres"]
        style = platebtn.PB_STYLE_GRADIENT
        for label in labels:
            btn = platebtn.PlateButton(self.panel, label=label, style=style)
            btn.SetPressColor(wx.Colour(208,208,208))
            btn_sizer.Add(btn, 0, wx.RIGHT|wx.LEFT|wx.CENTER, 5)

        top_sizer.Add(btn_sizer, 0, wx.ALL|wx.CENTER, 5)
        top_sizer.Add((1,1), 1, wx.EXPAND)
        self.panel.SetSizer(top_sizer)

        self.Center()
        self.Show()

    def OnSize(self, event):
        event.Skip()
        self.Refresh()

    def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self.panel)
        rect = self.panel.GetClientRect()
        dc.SetBackground(wx.Brush("white"))
        dc.Clear() 
        rect.SetHeight(30)
        dc.GradientFillLinear(rect, '#fbfbfb', '#efefef', wx.SOUTH)
        rect.SetTop(30)
        rect.SetHeight(2)
        dc.GradientFillLinear(rect, '#dbdbdb', '#c1c1c1', wx.SOUTH)

app = wx.App(0)
frame = GradientFrame(None, 'Test')
app.MainLoop()