当我运行我的wxPython应用程序(下面的代码)时,所有小部件似乎在窗口的左上角聚集在一起。我想要它,以便窗口与sizer的大小完全相同,但我似乎无法得到那个。
这就是我得到的:
这就是我想要的效果,减去糟糕的灰色背景:
有没有办法拉伸父面板以使其填满整个窗口?移除面板似乎给了我我想要的东西,但没有它,整个窗口变成单调的深灰色,从美学的角度来看看起来不自然,所以如果可能的话,我想保留它。
根据wx.version(),我在Python 2.7.6上使用版本3.0.0.0 msw(经典)。
class MyMenu(wx.Frame):
def __init__(self, parent, id, title):
super(MyMenu, self).__init__(parent, id, title)
valid_targets = ["jpg", "gif", "pcx",
"msp", "png", "ppm", "tiff"]
# Cheap Image Converter v .1
# File to convert: [ ] <Select File>
# Convert to: [.jpg]v
# [CONVERT]
self.convert_target = None
self.raw_file = []
self.panel = wx.Panel(self, -1)
# all elements will be declared in the order in which
# they appear, top to bottom, left to right.
self.intro_text = wx.StaticText(self.panel, -1, "Image Converter v 0.1")
# Load files button and binding
self.file_convert_text = wx.StaticText(self.panel, -1,
"Select a file to convert.")
self.file_text_box = wx.TextCtrl(self.panel, -1,
"Select a file!", style=wx.TE_READONLY)
self.load_file_button = wx.Button(self.panel, -1, "Load Files")
self.load_file_button.Bind(wx.EVT_BUTTON, self.SelectFileDialog)
# Conversion type selection box and label
self.choice_list_desc = wx.StaticText(self.panel, -1,
"Select format to convert to: ")
self.choice_list = wx.Choice(self.panel, -1,
choices=valid_targets)
self.choice_list.Bind(wx.EVT_CHOICE,
self.SelectFromChoice)
# Convert button!
self.go_button = wx.Button(self.panel, -1, "Convert!")
self.go_button.Bind(wx.EVT_BUTTON, self.PerformConversion)
# sizers!
self.master_container = wx.GridBagSizer(4, 3)
self.master_container.Add(self.intro_text, (0,0), (1,3), wx.EXPAND)
self.master_container.Add(self.file_convert_text, (1,0), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_RIGHT)
self.master_container.Add(self.file_text_box, (1,1), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_CENTER)
self.master_container.Add(self.load_file_button, (1,2), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_CENTER)
self.master_container.Add(self.choice_list_desc, (2,0), wx.DefaultSpan, wx.EXPAND)
self.master_container.Add(self.choice_list, (2,1), wx.DefaultSpan, wx.EXPAND)
self.master_container.Add(self.go_button, (3,0), wx.DefaultSpan, wx.EXPAND)
self.panel.SetSizerAndFit(self.master_container)
self.Centre()
# callbacks removed for brevity.
class MyApp(wx.App):
def OnInit(self):
frame = MyMenu(None, -1, 'Python Bulk Image Converter')
frame.Show(True)
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
答案 0 :(得分:0)
这在wxPython中有点令人困惑。就个人而言,我发现Fit
和SetSizerAndFit
方法非常烦人且不太有用。但是,由于这是您想要的,所以这样做的方法是更改代码中的以下行:
self.panel.SetSizerAndFit(self.master_container)
到此:
self.panel.SetSizer(self.master_container)
self.master_container.Fit(self)
这里是完整的代码(注意我注释掉了所有事件绑定,因为这些方法在你的例子中没有出现):
import wx
class MyMenu(wx.Frame):
def __init__(self, parent, id, title):
super(MyMenu, self).__init__(parent, id, title)
valid_targets = ["jpg", "gif", "pcx",
"msp", "png", "ppm", "tiff"]
# Cheap Image Converter v .1
# File to convert: [ ] <Select File>
# Convert to: [.jpg]v
# [CONVERT]
self.convert_target = None
self.raw_file = []
self.panel = wx.Panel(self, -1)
# all elements will be declared in the order in which
# they appear, top to bottom, left to right.
self.intro_text = wx.StaticText(self.panel, -1, "Image Converter v 0.1")
# Load files button and binding
self.file_convert_text = wx.StaticText(self.panel, -1,
"Select a file to convert.")
self.file_text_box = wx.TextCtrl(self.panel, -1,
"Select a file!", style=wx.TE_READONLY)
self.load_file_button = wx.Button(self.panel, -1, "Load Files")
#self.load_file_button.Bind(wx.EVT_BUTTON, self.SelectFileDialog)
# Conversion type selection box and label
self.choice_list_desc = wx.StaticText(self.panel, -1,
"Select format to convert to: ")
self.choice_list = wx.Choice(self.panel, -1,
choices=valid_targets)
#self.choice_list.Bind(wx.EVT_CHOICE,
#self.SelectFromChoice)
# Convert button!
self.go_button = wx.Button(self.panel, -1, "Convert!")
#self.go_button.Bind(wx.EVT_BUTTON, self.PerformConversion)
# sizers!
self.master_container = wx.GridBagSizer(4, 3)
self.master_container.Add(self.intro_text, (0,0), (1,3), wx.EXPAND)
self.master_container.Add(self.file_convert_text, (1,0), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_RIGHT)
self.master_container.Add(self.file_text_box, (1,1), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_CENTER)
self.master_container.Add(self.load_file_button, (1,2), wx.DefaultSpan, wx.EXPAND | wx.ALIGN_CENTER)
self.master_container.Add(self.choice_list_desc, (2,0), wx.DefaultSpan, wx.EXPAND)
self.master_container.Add(self.choice_list, (2,1), wx.DefaultSpan, wx.EXPAND)
self.master_container.Add(self.go_button, (3,0), wx.DefaultSpan, wx.EXPAND)
self.panel.Layout()
self.panel.SetSizer(self.master_container)
self.master_container.Fit(self)
self.Centre()
# callbacks removed for brevity.
class MyApp(wx.App):
def OnInit(self):
frame = MyMenu(None, -1, 'Python Bulk Image Converter')
frame.Show(True)
return True
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
此代码在Xubuntu 14.04,wxPython 2.8.12.1和Python 2.7上进行了测试