我在使用标志将2个按钮放在底部中心时遇到了一些困难。当我尝试时输出就是这个,我不明白它们为什么不与底部中心对齐。:
使用以下代码:
#!/usr/bin/python
# gui.py
import wx
import webbrowser
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
class PanelTwo(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
class GUI(wx.Frame):
def __init__(self, *args, **kwargs):
super(GUI, self).__init__(*args, **kwargs)
self.poo()
def poo(self):
self.CreateStatusBar()
menubar = wx.MenuBar()
fileMenu = wx.Menu()
self.add_entry = wx.MenuItem(fileMenu, -1, '&Add Entry\tShift+A', 'Add employee')
quit = wx.MenuItem(fileMenu, wx.ID_EXIT, '&Quit\tEsc', 'Quit application')
fileMenu.AppendItem(self.add_entry)
fileMenu.AppendItem(quit)
menubar.Append(fileMenu, '&Options')
self.SetMenuBar(menubar)
self.panel_one = PanelOne(self)
self.panel_two = PanelTwo(self)
self.panel_two.Hide()
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.panel_one, 1, wx.EXPAND)
vbox.Add(self.panel_two, 1, wx.EXPAND)
self.SetSizer(vbox)
pv1box = wx.BoxSizer(wx.HORIZONTAL)
self.panel_one.SetSizer(pv1box)
self.chrome_button = wx.Button(self.panel_one, id=2, label='Gmail')
self.outlook_button = wx.Button(self.panel_one, id=1, label='Outlook')
pv1box.Add(self.chrome_button, flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER | wx.ALIGN_RIGHT)
pv1box.Add(self.outlook_button, flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER | wx.ALIGN_RIGHT)
self.SetSize((450, 300))
self.SetTitle('Prototype')
self.Centre()
self.Show(True)
self.Bind(wx.EVT_MENU, self.onQuit, quit)
self.Bind(wx.EVT_KEY_DOWN, self.onESCQuit, quit)
self.Bind(wx.EVT_MENU, self.onSwitchPanels, self.add_entry)
self.chrome_button.Bind(wx.EVT_BUTTON, self.onClick)
self.outlook_button.Bind(wx.EVT_BUTTON, self.onClick)
def onQuit(self, e):
dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
input = dial.ShowModal()
if input == wx.ID_YES:
self.Close()
def onESCQuit(self, e):
esc = e.GetKey()
if esc == wx.WXK_ESCAPE:
dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question', wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
input = dial.ShowModal()
if input == wx.ID_YES:
self.Close()
def onSwitchPanels(self, event):
if self.panel_one.IsShown():
self.add_entry.SetText('Notifications')
self.panel_one.Hide()
self.panel_two.Show()
else:
self.add_entry.SetText('Add Entry')
self.panel_one.Show()
self.panel_two.Hide()
def onClick(self, event):
if event.Id == self.chrome_button.Id:
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# Without this path it will open in Internet Explorer
webbrowser.get(chrome_path).open('https://mail.google.com')
else:
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
# Without this path it will open in Internet Explorer
webbrowser.get(chrome_path).open('https://outlook.live.com')
def main():
app = wx.App()
GUI(None)
app.MainLoop()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
框架上有一个单独的分级器,两个面板是框架的父级,所以这个分级器将它们定位在框架上。 两个按钮是每个面板的父级,但框架大小调整器在面板上时试图将它们定位在框架上。 面板每个都需要一个分级器,这样他们就可以将按钮放在面板上。
你有
Frame
--->vbox -> set to frame
--->panel_one -> added to vbox
------->chrome_button -> added to vbox
--->panel_two -> added to vbox
------->outlook_button -> added to vbox
你需要
Frame
--->vbox -> set to frame
--->panel_one -> added to vbox
------->p1vbox(new sizer) -> set to panel_one
------->chrome_button -> added to p1vbox
--->panel_two -> added to vbox
------->p2vbox(new sizer) -> set to panel_two
------->outlook_button -> added to p2vbox
使用新代码在按钮的任一侧添加一个拉伸垫片,使它们位于中心位置。
pv1box.AddStretchSpacer()
pv1box.Add(self.chrome_button, flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER | wx.ALIGN_RIGHT)
pv1box.Add(self.outlook_button, flag=wx.ALIGN_BOTTOM | wx.ALIGN_CENTER | wx.ALIGN_RIGHT)
pv1box.AddStretchSpacer()