wxpython:使用sizer对中按钮
也许主题应该是如何给尺码添加背景色,但无论如何……
谁能帮帮我,我需要有"按钮的背景颜色",例如蓝色和红色,然后使用尺码来居中按钮…如果我能为boxsizer=p设置背景色,这项任务就会容易得多。
所以,请试着在不去除红色和蓝色背景的情况下,将这些按钮放在尺码的中央
选择 | 换行 | 行号
- import wx
- class MyFrame(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, id=-1, size=(300,150))
- frame_panel = wx.Panel(self, -1)
- button1_panel = wx.Panel(frame_panel, -1) #panel for button1
- button1_panel.SetBackgroundColour('BLUE')
- button2_panel = wx.Panel(frame_panel, -1) #panel for button2
- button2_panel.SetBackgroundColour('RED')
- button1 = wx.Button(button1_panel, id=-1, label="button1", size=(80,20))
- button2 = wx.Button(button2_panel, id=-1, label="button2", size=(80,20))
- button1_sizer = wx.BoxSizer(wx.HORIZONTAL) #add button1 to sizer
- button1_sizer.Add(button1, 0, wx.ALIGN_CENTER)
- button2_sizer = wx.BoxSizer(wx.HORIZONTAL) #add button2 to sizer
- button2_sizer.Add(button1, 0, wx.ALIGN_CENTER)
- button1_panel.SetSizer(button1_sizer) #set sizers for panel
- button2_panel.SetSizer(button2_sizer)
- hbox = wx.BoxSizer(wx.HORIZONTAL) #add panels to "main sizer"
- hbox.Add(button1_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
- hbox.Add(button2_panel, 1, wx.EXPAND|wx.ALIGN_CENTER_HORIZONTAL)
- frame_panel.SetAutoLayout(True)
- frame_panel.SetSizer(hbox)
- frame_panel.Layout()
- app = wx.PySimpleApp()
- MyFrame().Show()
- app.MainLoop()
如果你想从"开始"开始,这里的代码没有背景颜色,只需在这里插入第一个例子中的颜色(并使用SIZER)
选择 | 换行 | 行号
- #!/usr/bin/env python
- # -*- coding: UTF-8 -*-
- import wx
- class MyFrame(wx.Frame):
- def __init__(self):
- wx.Frame.__init__(self, None, id=-1, size=(300,150))
- frame_panel = wx.Panel(self, -1)
- button1 = wx.Button(frame_panel, id=-1, label="button1", size=(80,20))
- button2 = wx.Button(frame_panel, id=-1, label="button2", size=(80,20))
- hbox = wx.BoxSizer(wx.HORIZONTAL)
- hbox.Add(button1, 1, wx.ALIGN_CENTER|wx.ALL, 25)
- hbox.Add(button2, 1, wx.ALIGN_CENTER|wx.ALL, 25)
- frame_panel.SetAutoLayout(True)
- frame_panel.SetSizer(hbox)
- frame_panel.Layout()
- app = wx.PySimpleApp()
- MyFrame().Show()
- app.MainLoop()