gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:09:03 25 4
gpt4 key购买 nike

我正在使用 wxWidgets (wxPython) 为我的 python 项目构建 GUI。有一个小但烦人的问题我似乎无法弄清楚。开始、交叉和浏览这三个按钮应该位于窗口的底部。这些是 wx.ToggleButton 小部件,它们属于水平 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()

最佳答案

大小调整器中的比例值到处都是,在某种程度上标志条目也是如此。

  1. 清理比例
  2. 添加“NEXT”和“BACK”按钮(您忘记将它们添加到 sizer 中)
  3. leftmost_box 添加一个虚拟项目,比例为 1(以强制最终项目位于底部)
  4. leftmost_box 添加到主 sizer 时设置 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

关于python - 如何在 wxPython 中强制关闭小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51813392/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com