我正在尝试研究wxPython和sizer,并将以下示例放在一起:
import wx
class MyTestFrame(wx.Frame):
def __init__(self, parent, title):
super(MyTestFrame, self).__init__(parent, title=title,
size=(250, 150))
# the master panel of the frame - "Add a panel so it looks correct on all platforms"
self.panel = wx.Panel(self, wx.ID_ANY)
# self.panel.SetBackgroundColour(wx.Colour(124, 224, 124)) # to confirm the square is the panel
# want these buttons absolutely positioned
btn_A = wx.Button(self, id=1, label='A', pos=(10, 10), size=(30, 30))
btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
btn_B = wx.Button(self, id=2, label='B', pos=(45, 10), size=(30, 30))
btn_C = wx.Button(self, id=3, label='C', pos=(80, 10), size=(30, 30))
# additional object
mastersizer = wx.BoxSizer(wx.VERTICAL)
btnsizer = wx.BoxSizer(wx.HORIZONTAL)
btnsizer.Add(btn_A, 0)
btnsizer.Add(btn_B, 0)
btnsizer.Add(btn_C, 0)
mastersizer.Add(btnsizer, 1, wx.EXPAND)
self.panel.SetSizer(mastersizer)
#~ mastersizer.Fit(self) # makes the window as large as the buttons
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
MyTestFrame(None, 'Test')
app.MainLoop()
当我运行时,我会在图像上看到一个窗口:
有人可以解释一下,为什么我会在左上角找到那个灰色的小方块 - 以及实现代码的正确方法是什么? (按钮是故意上色的,所以很明显......)我在Ubuntu Lucid上,以防这是特定于平台的。
编辑:这个方块显然是面板本身,但后来我不知道它为什么不调整大小并成为按钮的“父”按预期?答案 0 :(得分:5)
# want these buttons absolutely positioned
# must be children of panel - if panel is to encompass them!
btn_A = wx.Button(self.panel, id=1, label='A', pos=(10, 10), size=(30, 30))
btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
btn_B = wx.Button(self.panel, id=2, label='B', pos=(45, 10), size=(30, 30))
btn_C = wx.Button(self.panel, id=3, label='C', pos=(80, 10), size=(30, 30))
然后一切似乎都很好:
嗯,很抱歉在这里浪费了空间 - 但希望可能对其他人有用:)
干杯!