如何在wxPython中强制关闭小部件

2024-09-28 05:27:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用wxWidgets(wxPython)为我的python项目构建GUI。有一个小问题,但恼人的,我似乎无法解决。“开始”、“交叉”和“浏览”三个按钮应位于窗口底部。这些是wx.ToggleButton小部件,它们属于水平BoxSizer,而水平BoxSizer又是名为leftmost_box的垂直BoxSizer的子级。 问题是,我该如何把这些按钮按到最左边的角落?我试着设置标志,即wx.ALIGN_BOTTOM,但是没有用。你知道吗

附加源代码也。你知道吗

Screenshot

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()

Tags: theboxsizestaticbuttonalllabelcentral
1条回答
网友
1楼 · 发布于 2024-09-28 05:27:26

sizers中的proportion值到处都是,flag条目也在一定程度上是如此。你知道吗

  1. 清理proportion
  2. 添加“下一步”和“上一步”按钮(您忘了将它们添加到尺寸器中)
  3. leftmost_box添加一个比例为1的虚拟项(强制最后一个项位于底部)
  4. 将扩展标志添加到主大小器时,在leftmost_box上设置扩展标志

我想就这些。你知道吗

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

相关问题 更多 >

    热门问题