如何在wxPython中强制使用小部件

时间:2018-08-12 22:32:15

标签: python user-interface wxpython

我正在使用wxWidgets(wxPython)为我的python项目构建GUI。我似乎无法弄清一个小但烦人的问题。三个按钮“开始”,“交叉”和“浏览”应位于窗口底部。这些是wx.ToggleButton小部件,它们属于水平BoxSizer,而水平BoxSizer则是名为leftmost_box的垂直wx.ALIGN_BOTTOM的子代。 问题是,如何将这些按钮向下压到最左角?我尝试设置标志,即import wx ################################# # Set up the application window # ################################# app = wx.App() mainframe = wx.Frame(None, -1) mainframe.SetSize((900, 500)) mainframe.SetTitle('Mdl') panel = wx.Panel(mainframe, -1) panel.SetBackgroundColour('grey') # Setting the default font # #wx.Font(pointSize, family, style, weight, faceName="") panel.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.LIGHT, faceName = 'Helvetica')) #################### # Global variables # #################### choices = ['SAT Exam', 'Top 300', 'Custom Dictionary', 'Advanced English'] word_of_the_day = wx.StaticText(panel, label = 'Prowess: skill or expertise in a particular activity or field: her culinary prowess.', size = (180, 190)) #Throwaway var for testing _word_of_the_day = wx.StaticText(panel, label = 'Prowess: skill or expertise in a particular activity or field: her culinary prowess.', size = (180, 190)) #################### # Controls go here # #################### searchbar_wgt = wx.SearchCtrl(panel, value = 'Search...', size = (210, -1)) pick_active_dictionary_wgt = wx.Choice(panel, choices = choices, size = (170, -1)) active_dictionary_size_wgt = wx.SpinCtrl(panel, min = 0, max = 50, initial = 15, size = (50, -1)) button_next_wgt = wx.Button(panel, label = 'Next >') button_back_wgt = wx.Button(panel, label = '< Back') set_word_order_wgt = wx.RadioBox(panel, label = 'Order', choices = ['Normal', 'Random']) logo_image = wx.Image('logo.png', wx.BITMAP_TYPE_ANY).Scale(285, 63) bitmap_logo_image = wx.StaticBitmap(panel, -1, logo_image.ConvertToBitmap()) start_mode_button_wgt = wx.ToggleButton(panel, label = 'Start') crossword_mode_button_wgt = wx.ToggleButton(panel, label = 'Cross') browse_mode_button_wgt = wx.ToggleButton(panel, label = 'Browse') ################### # Standard Layout # ################### main_box = wx.BoxSizer(wx.HORIZONTAL) leftmost_box = wx.BoxSizer(wx.VERTICAL) central_box = wx.BoxSizer(wx.VERTICAL) rightmost_box = wx.BoxSizer(wx.VERTICAL) ############# # Subsizers # ############# leftmost_box_subsizer = wx.BoxSizer(wx.HORIZONTAL) static_box_central = wx.StaticBox(panel, label = 'Card #', size = (180, 180)) static_box_szr_central = wx.StaticBoxSizer(static_box_central, wx.VERTICAL) static_box_left = wx.StaticBox(panel, label = 'Vocabulary Options') static_box_szr_left = wx.StaticBoxSizer(static_box_left, wx.VERTICAL) static_box_right = wx.StaticBox(panel, label = 'Word of the Day', size = (200, 200)) static_box_szr_right = wx.StaticBoxSizer(static_box_right, wx.VERTICAL) static_box_szr_right.Add(word_of_the_day, 1, wx.ALL, 5) static_box_szr_left.AddMany([ ( pick_active_dictionary_wgt, 1, wx.ALL, 5 ), ( active_dictionary_size_wgt, 1, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 5 ), ( set_word_order_wgt, 1, wx.LEFT|wx.RIGHT|wx.ALIGN_RIGHT, 5 ) ]) # Changes the Label (NB: StaticBox can be reused to display definitions) # static_box.SetLabel('Definition') # To create an SB widget, you need to create an SBS and pass an SB to it as an argument # It will then behave as a regular sizer main_box.AddMany([ ( leftmost_box, 1, wx.ALL, 10 ), ( central_box, 1, wx.TOP|wx.BOTTOM, 10 ), ( rightmost_box, 1, wx.ALL, 10 ) ]) static_box_szr_central.Add(_word_of_the_day, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5) # Building the rightmost box rightmost_box.Add(searchbar_wgt, 1, wx.ALIGN_RIGHT) rightmost_box.Add(static_box_szr_right, 8, wx.TOP|wx.ALIGN_RIGHT, 50) # Building the leftmost box leftmost_box.Add(static_box_szr_left, 2, wx.ALL|wx.ALIGN_LEFT, 10) leftmost_box.Add(leftmost_box_subsizer, 1, wx.ALL|wx.EXPAND, 10) # Building the leftmost subsizer leftmost_box_subsizer.AddMany([ ( start_mode_button_wgt, wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 ), ( crossword_mode_button_wgt, wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 ), ( browse_mode_button_wgt, wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 ) ]) #Toggle the start mode button start_mode_button_wgt.SetValue(True) # The pLexis logo central_box.Add(bitmap_logo_image, 1, wx.ALL|wx.ALIGN_CENTRE, 5) # The word card central_box.Add(static_box_szr_central, 2, wx.ALL|wx.ALIGN_CENTRE, 5) # Kick it! panel.SetSizer(main_box) mainframe.Show() app.MainLoop() ,但不起作用。

还附上源代码。

Screenshot

before

1 个答案:

答案 0 :(得分:0)

您在定尺寸器中的proportion值遍及整个地方,并且在一定程度上flag条目也是如此。

  1. 清理proportion
  2. 添加NEXT和BACK按钮(您已忘记将它们添加到尺寸调整器中)
  3. 将虚拟物品添加到leftmost_box中,比例为= 1(将最终物品逼到底部)
  4. leftmost_box上的EXPAND标志添加到主尺寸调整器中

我认为仅此而已。

import wx

#################################
# Set up the application window #
#################################

app = wx.App()
mainframe = wx.Frame(None, -1)
mainframe.SetSize((900, 500))
mainframe.SetTitle('Mdl')
panel = wx.Panel(mainframe, -1)
panel.SetBackgroundColour('grey')

# Setting the default font #
#wx.Font(pointSize, family, style,  weight, faceName="")
panel.SetFont(wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.LIGHT, faceName = 'Helvetica'))


####################
# Global variables #
####################

choices = ['SAT Exam', 'Top 300', 'Custom Dictionary', 'Advanced English']
word_of_the_day   = wx.StaticText(panel, label = 'Prowess: skill or expertise in a particular activity or field: her culinary prowess.', size = (180, 190))

#Throwaway var for testing
_word_of_the_day   = wx.StaticText(panel, label = 'Prowess: skill or expertise in a particular activity or field: her culinary prowess.', size = (180, 190))

####################
# Controls go here #
####################

searchbar_wgt                = wx.SearchCtrl(panel, value = 'Search...', size = (210, -1))
pick_active_dictionary_wgt   = wx.Choice(panel, choices = choices, size = (170, -1))
active_dictionary_size_wgt   = wx.SpinCtrl(panel, min = 0, max = 50, initial = 15, size = (50, -1))
button_next_wgt              = wx.Button(panel, label = 'Next >')
button_back_wgt              = wx.Button(panel, label = '< Back')
set_word_order_wgt           = wx.RadioBox(panel, label = 'Order', choices = ['Normal', 'Random'])
logo_image                   = wx.Image('logo.png', wx.BITMAP_TYPE_ANY).Scale(285, 63)
bitmap_logo_image            = wx.StaticBitmap(panel, -1, logo_image.ConvertToBitmap())
start_mode_button_wgt        = wx.ToggleButton(panel, label = 'Start')
crossword_mode_button_wgt    = wx.ToggleButton(panel, label = 'Cross')
browse_mode_button_wgt       = wx.ToggleButton(panel, label = 'Browse')


###################
# Standard Layout #
###################

main_box      = wx.BoxSizer(wx.HORIZONTAL)
leftmost_box  = wx.BoxSizer(wx.VERTICAL)
central_box   = wx.BoxSizer(wx.VERTICAL)
rightmost_box = wx.BoxSizer(wx.VERTICAL)

#############
# Subsizers #
#############
leftmost_box_subsizer   = wx.BoxSizer(wx.HORIZONTAL)

static_box_central      = wx.StaticBox(panel, label = 'Card #', size = (180, 180))
static_box_szr_central  = wx.StaticBoxSizer(static_box_central, wx.VERTICAL)

static_box_left         = wx.StaticBox(panel, label = 'Vocabulary Options')
static_box_szr_left     = wx.StaticBoxSizer(static_box_left, wx.VERTICAL)

static_box_right        = wx.StaticBox(panel, label = 'Word of the Day', size = (200, 200))
static_box_szr_right    = wx.StaticBoxSizer(static_box_right, wx.VERTICAL)


static_box_szr_right.Add(word_of_the_day, 0, wx.ALL, 5)
static_box_szr_left.AddMany([
                           (    pick_active_dictionary_wgt, 0, wx.ALL, 5 ),
                           (    active_dictionary_size_wgt, 0, wx.LEFT|wx.RIGHT|wx.BOTTOM|wx.ALIGN_RIGHT, 5 ),
                           (    set_word_order_wgt, 0, wx.LEFT|wx.RIGHT|wx.ALIGN_RIGHT, 5 )
                           ])

# Changes the Label (NB: StaticBox can be reused to display definitions)
# static_box.SetLabel('Definition')
# To create an SB widget, you need to create an SBS and pass an SB to it as an argument
# It will then behave as a regular sizer

main_box.AddMany([
                (   leftmost_box,   0, wx.EXPAND|wx.ALL, 10 ),
                (   central_box,    0, wx.TOP|wx.BOTTOM, 10 ),
                (   rightmost_box,  0, wx.ALL,           10 )
                ])

static_box_szr_central.Add(_word_of_the_day, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)

# Building the rightmost box
rightmost_box.Add(button_next_wgt, 0, wx.ALL|wx.ALIGN_LEFT, 10)
rightmost_box.Add(searchbar_wgt, 0, wx.ALIGN_RIGHT)
rightmost_box.Add(static_box_szr_right, 0, wx.TOP|wx.ALIGN_RIGHT, 50)

# Building the leftmost box
leftmost_box.Add(button_back_wgt, 0, wx.ALL|wx.ALIGN_LEFT, 10)
leftmost_box.Add(static_box_szr_left, 0, wx.ALL|wx.ALIGN_LEFT, 10)
leftmost_box.Add((-1,-1), proportion=1)
leftmost_box.Add(leftmost_box_subsizer, 0, wx.ALL|wx.EXPAND, 10)

# Building the leftmost subsizer
leftmost_box_subsizer.AddMany([
                             (  start_mode_button_wgt,     wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 ),
                             (  crossword_mode_button_wgt, wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 ),
                             (  browse_mode_button_wgt,    wx.TOP|wx.BOTTOM|wx.ALIGN_BOTTOM, 10 )
                             ])

#Toggle the start mode button
start_mode_button_wgt.SetValue(True)

# The pLexis logo
central_box.Add(bitmap_logo_image, 0, wx.ALL|wx.ALIGN_CENTRE, 5)
# The word card
central_box.Add(static_box_szr_central, 0, wx.ALL|wx.ALIGN_CENTRE, 5)

# Kick it!
panel.SetSizer(main_box)
mainframe.Show()
app.MainLoop()

enter image description here