我的代码有什么问题吗?我在主框架中的1个boxsizer中使用了3个sizer。底部大小调整器用于绘图,另外两个用于用户交互。现在最重要的问题在于3个sizer之间存在太多的空白区域。我希望我的GUI看起来更紧凑,如果可能的话,绘图区域应该更大。
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Oct 8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import os
import wx
import wx.xrc
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigureCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
#from numpy import arange, sin, pi
###########################################################################
## Class MainFrame
###########################################################################
class MainFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"计算机插值 v0.1", pos = wx.DefaultPosition, size = wx.Size( 508,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.FRAME_SHAPED|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ) )
####################################################################
self.MainMenu = wx.MenuBar( 0 )
self.File = wx.Menu()
self.SaveAs = wx.MenuItem( self.File, wx.ID_ANY, u"另存为...", wx.EmptyString, wx.ITEM_NORMAL )
self.File.AppendItem( self.SaveAs )
self.Exit = wx.MenuItem( self.File, wx.ID_ANY, u"退出...", wx.EmptyString, wx.ITEM_NORMAL )
self.File.AppendItem( self.Exit )
self.MainMenu.Append( self.File, u"文件" )
self.About_M = wx.Menu()
self.About = wx.MenuItem( self.About_M, wx.ID_ANY, u"关于", wx.EmptyString, wx.ITEM_NORMAL )
self.About_M.AppendItem( self.About )
self.MainMenu.Append( self.About_M, u"关于" )
self.SetMenuBar( self.MainMenu )
####################################################################
MainSizer = wx.BoxSizer( wx.VERTICAL )
self.MainPanel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
UserSizer = wx.BoxSizer( wx.VERTICAL )
upperSizer = wx.BoxSizer( wx.HORIZONTAL )
####################################################################
self.TextExplanation = wx.StaticText( self.MainPanel, wx.ID_ANY, u"请在下面输入框内输入要插值的数,以空格间隔。\n输入范围在(1.2, 2.0)。", wx.DefaultPosition, wx.DefaultSize, 0 )
self.TextExplanation.Wrap( -1 )
upperSizer.Add( self.TextExplanation, 0, wx.ALL, 20 )
upperSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
RadioBoxInterpolationChoices = [ u"线性插值", u"抛物线插值" ]
self.RadioBoxInterpolation = wx.RadioBox( self.MainPanel, wx.ID_ANY, u"插值种类", wx.DefaultPosition, wx.DefaultSize, RadioBoxInterpolationChoices, 1, wx.RA_SPECIFY_COLS )
self.RadioBoxInterpolation.SetSelection( 1 )
upperSizer.Add( self.RadioBoxInterpolation, 0, wx.ALL, 5 )
UserSizer.Add( upperSizer, 1, wx.ALL|wx.EXPAND|wx.SHAPED, 5 )
lowerSizer = wx.BoxSizer( wx.HORIZONTAL )
self.TextCtrVal = wx.TextCtrl( self.MainPanel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PROCESS_ENTER )
lowerSizer.Add( self.TextCtrVal, 0, wx.ALL, 20 )
lowerSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.ButtonCal = wx.Button( self.MainPanel, wx.ID_ANY, u"计算", wx.DefaultPosition, wx.DefaultSize, 0 )
lowerSizer.Add( self.ButtonCal, 0, wx.ALL, 20 )
self.ButtonCls = wx.Button( self.MainPanel, wx.ID_ANY, u"清空", wx.DefaultPosition, wx.DefaultSize, 0 )
lowerSizer.Add( self.ButtonCls, 0, wx.ALL, 20 )
UserSizer.Add( lowerSizer, 1, wx.EXPAND|wx.SHAPED, 5 )
PlotSizer = wx.BoxSizer( wx.VERTICAL )
#################################################
p1 = PlotPanel(self)
PlotSizer.Add(p1, 1, wx.EXPAND |wx.ALL,1)
#################################################
UserSizer.Add( PlotSizer, 1, wx.EXPAND, 5 )
self.MainPanel.SetSizer( UserSizer )
self.MainPanel.Layout()
UserSizer.Fit( self.MainPanel )
MainSizer.Add( self.MainPanel, 1, wx.EXPAND |wx.ALL, 1 )
self.SetSizer( MainSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Bind( wx.EVT_MENU, self.SaveAsFile, id = self.SaveAs.GetId() )
self.Bind( wx.EVT_MENU, self.ExitNow, id = self.Exit.GetId() )
self.Bind( wx.EVT_MENU, self.Dialog, id = self.About.GetId() )
self.TextCtrVal.Bind( wx.EVT_TEXT_ENTER, self.Calculate )
self.ButtonCal.Bind( wx.EVT_BUTTON, self.Calcualte )
self.ButtonCls.Bind( wx.EVT_BUTTON, self.ClearPlot )
# Necessary varibles
selection = self.RadioBoxInterpolation.GetSelection()
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def SaveAsFile( self, event ):
event.Skip()
def ExitNow( self, event ):
self.Close(True)
def Dialog( self, event ):
dlg = wx.MessageDialog( self, u"Test", u"Test", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def Calculate( self, event ):
event.Skip()
def Calcualte( self, event ):
event.Skip()
def ClearPlot( self, event ):
event.Skip()
###########################################################################
## Class PlotPanel
###########################################################################
class PlotPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent, -1,size=(300,530))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure = matplotlib.figure.Figure(figsize=(5,4))
self.axes = self.figure.add_subplot(111)
self.y_max = 2.0
self.canvas = FigureCanvas(self,-1,self.figure)
self.sizer.Add(self.canvas,proportion =1, border = 5,flag = wx.ALL | wx.EXPAND)
self.SetSizer(self.sizer)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = MainFrame(None)
app.frame.Show()
app.MainLoop()
答案 0 :(得分:0)
我改变了两行代码,并认为它可能就是你要找的东西。
UserSizer.Add( upperSizer, 0, wx.EXPAND, 5 )
和
UserSizer.Add( lowerSizer, 0, 0, 5 )
以下是更新后的代码:
# -*- coding: utf-8 -*-
###########################################################################
## Python code generated with wxFormBuilder (version Oct 8 2012)
## http://www.wxformbuilder.org/
##
## PLEASE DO "NOT" EDIT THIS FILE!
###########################################################################
import os
import wx
import wx.xrc
import matplotlib
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
FigureCanvasWxAgg as FigureCanvas, \
NavigationToolbar2WxAgg as NavigationToolbar
#from numpy import arange, sin, pi
###########################################################################
## Class MainFrame
###########################################################################
class MainFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"计算机插值 v0.1", pos = wx.DefaultPosition, size = wx.Size( 508,530 ), style = wx.DEFAULT_FRAME_STYLE|wx.FRAME_SHAPED|wx.TAB_TRAVERSAL )
self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
self.SetForegroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_3DLIGHT ) )
####################################################################
self.MainMenu = wx.MenuBar( 0 )
self.File = wx.Menu()
self.SaveAs = wx.MenuItem( self.File, wx.ID_ANY, u"另存为...", wx.EmptyString, wx.ITEM_NORMAL )
self.File.AppendItem( self.SaveAs )
self.Exit = wx.MenuItem( self.File, wx.ID_ANY, u"退出...", wx.EmptyString, wx.ITEM_NORMAL )
self.File.AppendItem( self.Exit )
self.MainMenu.Append( self.File, u"文件" )
self.About_M = wx.Menu()
self.About = wx.MenuItem( self.About_M, wx.ID_ANY, u"关于", wx.EmptyString, wx.ITEM_NORMAL )
self.About_M.AppendItem( self.About )
self.MainMenu.Append( self.About_M, u"关于" )
self.SetMenuBar( self.MainMenu )
####################################################################
MainSizer = wx.BoxSizer( wx.VERTICAL )
self.MainPanel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
UserSizer = wx.BoxSizer( wx.VERTICAL )
upperSizer = wx.BoxSizer( wx.HORIZONTAL )
####################################################################
self.TextExplanation = wx.StaticText( self.MainPanel, wx.ID_ANY, u"请在下面输入框内输入要插值的数,以空格间隔。\n输入范围在(1.2, 2.0)。", wx.DefaultPosition, wx.DefaultSize, 0 )
self.TextExplanation.Wrap( -1 )
upperSizer.Add( self.TextExplanation, 0, wx.ALL, 20 )
upperSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
RadioBoxInterpolationChoices = [ u"线性插值", u"抛物线插值" ]
self.RadioBoxInterpolation = wx.RadioBox( self.MainPanel, wx.ID_ANY, u"插值种类", wx.DefaultPosition, wx.DefaultSize, RadioBoxInterpolationChoices, 1, wx.RA_SPECIFY_COLS )
self.RadioBoxInterpolation.SetSelection( 1 )
upperSizer.Add( self.RadioBoxInterpolation, 0, wx.ALL, 5 )
UserSizer.Add( upperSizer, 0, wx.EXPAND, 5 )
lowerSizer = wx.BoxSizer( wx.HORIZONTAL )
self.TextCtrVal = wx.TextCtrl( self.MainPanel, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, wx.TE_PROCESS_ENTER )
lowerSizer.Add( self.TextCtrVal, 0, wx.ALL, 20 )
lowerSizer.AddSpacer( ( 0, 0), 1, wx.EXPAND, 5 )
self.ButtonCal = wx.Button( self.MainPanel, wx.ID_ANY, u"计算", wx.DefaultPosition, wx.DefaultSize, 0 )
lowerSizer.Add( self.ButtonCal, 0, wx.ALL, 20 )
self.ButtonCls = wx.Button( self.MainPanel, wx.ID_ANY, u"清空", wx.DefaultPosition, wx.DefaultSize, 0 )
lowerSizer.Add( self.ButtonCls, 0, wx.ALL, 20 )
UserSizer.Add( lowerSizer, 0, 0, 5 )
PlotSizer = wx.BoxSizer( wx.VERTICAL )
#################################################
p1 = PlotPanel(self)
PlotSizer.Add(p1, 1, wx.EXPAND |wx.ALL,1)
#################################################
UserSizer.Add( PlotSizer, 1, wx.EXPAND, 5 )
self.MainPanel.SetSizer( UserSizer )
self.MainPanel.Layout()
UserSizer.Fit( self.MainPanel )
MainSizer.Add( self.MainPanel, 1, wx.EXPAND |wx.ALL, 1 )
self.SetSizer( MainSizer )
self.Layout()
self.Centre( wx.BOTH )
# Connect Events
self.Bind( wx.EVT_MENU, self.SaveAsFile, id = self.SaveAs.GetId() )
self.Bind( wx.EVT_MENU, self.ExitNow, id = self.Exit.GetId() )
self.Bind( wx.EVT_MENU, self.Dialog, id = self.About.GetId() )
self.TextCtrVal.Bind( wx.EVT_TEXT_ENTER, self.Calculate )
self.ButtonCal.Bind( wx.EVT_BUTTON, self.Calcualte )
self.ButtonCls.Bind( wx.EVT_BUTTON, self.ClearPlot )
# Necessary varibles
selection = self.RadioBoxInterpolation.GetSelection()
def __del__( self ):
pass
# Virtual event handlers, overide them in your derived class
def SaveAsFile( self, event ):
event.Skip()
def ExitNow( self, event ):
self.Close(True)
def Dialog( self, event ):
dlg = wx.MessageDialog( self, u"Test", u"Test", wx.OK)
dlg.ShowModal()
dlg.Destroy()
def Calculate( self, event ):
event.Skip()
def Calcualte( self, event ):
event.Skip()
def ClearPlot( self, event ):
event.Skip()
###########################################################################
## Class PlotPanel
###########################################################################
class PlotPanel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent, -1,size=(300,530))
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.figure = matplotlib.figure.Figure(figsize=(5,4))
self.axes = self.figure.add_subplot(111)
self.y_max = 2.0
self.canvas = FigureCanvas(self,-1,self.figure)
self.sizer.Add(self.canvas,proportion =1, border = 5,flag = wx.ALL | wx.EXPAND)
self.SetSizer(self.sizer)
if __name__ == '__main__':
app = wx.PySimpleApp()
app.frame = MainFrame(None)
app.frame.Show()
app.MainLoop()