我正在尝试学习wxPython并遇到布局问题。我尝试将小部件集放在单独的面板中,以在将来管理它们的位置\大小。但是由于某些原因,对于“顶部块”面板工作正常的代码不适用于“ Mods”面板。
我搜索了Layout()函数,但是由于某些原因,它在“顶部块”和“模块”中的行为有所不同。无需重新排列小部件,只需将它们全部放在面板顶部的细线中即可。我无法发布图片,但可以提供屏幕截图。
class ItemInfo(wx.Frame):
def __init__(self, parent, item, *args):
super(ItemInfo, self).__init__(parent)
self.Size = wx.Size(500, 600)
self.current_item = self.find_current_item(item)
common_item_types = Storage.ItemStorage.get_common_item_keys()
# self.Bind(wx.EVT_SHOW, self.show_current_item_stats)
mod_list = []
mod_list.append("None")
for key, value in Storage.item_storage.mod_dict.items():
mod_list.append(key)
# Главная панель
main_panel = wx.Panel(self)
main_panel.SetBackgroundColour('#484f49')
##============================= Шрифты ===============================##
label_font = wx.Font(16, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
sublabel_font = wx.Font(12, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_BOLD, False)
stat_font = wx.Font(16, wx.FONTFAMILY_ROMAN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL, False)
# ============================= Заголовок ======================================
label_panel = wx.Panel(main_panel, pos=(0, 0))
label_panel.SetSize(500, 30)
label_panel.SetBackgroundColour('#46f057')
label_text = wx.StaticText(label_panel, label="Item information")
label_text.SetFont(label_font)
label_text.SetForegroundColour('#6b756c')
label_text.SetPosition((170, 5))
##================================ Top block ==========================
top_panel = wx.Panel(main_panel, pos=(10, 30))
top_panel.SetSize(470, 70)
top_panel.SetBackgroundColour('#1271e6')
top_sizer = wx.BoxSizer(wx.HORIZONTAL)
# Создаем коробку с именем предмета
item_name_sizer = wx.BoxSizer(wx.VERTICAL)
item_name_label = wx.StaticText(top_panel, label="Current item name")
item_name_label.SetFont(sublabel_font)
item_name_ctrl = wx.TextCtrl(top_panel, value=self.current_item.name)
item_name_sizer.AddSpacer(7)
item_name_sizer.Add(item_name_label, 0, wx.ALIGN_CENTER_HORIZONTAL)
item_name_sizer.AddSpacer(5)
item_name_sizer.Add(item_name_ctrl, wx.EXPAND)
top_sizer.Add(item_name_sizer, 0, wx.LEFT | wx.RIGHT, border=5)
# Создаем коробку с типом предмета
item_key_sizer = wx.BoxSizer(wx.VERTICAL)
item_key_label = wx.StaticText(top_panel, label="Item key")
item_key_label.SetFont(sublabel_font)
item_key_cbox = wx.ComboBox(top_panel, value=self.current_item.key, choices=common_item_types)
item_key_sizer.AddSpacer(7)
item_key_sizer.Add(item_key_label, 0, wx.ALIGN_CENTER_HORIZONTAL)
item_key_sizer.AddSpacer(5)
item_key_sizer.Add(item_key_cbox, wx.EXPAND)
top_sizer.Add(item_key_sizer, 0, wx.LEFT | wx.RIGHT, border=5)
# Создаем коробку с редкостью предмета
item_rarity_sizer = wx.BoxSizer(wx.VERTICAL)
item_rarity_label = wx.StaticText(top_panel, label="Item rarity")
item_rarity_label.SetFont(sublabel_font)
item_rarity_cbox = wx.ComboBox(top_panel, value=self.current_item.rarity.key, choices=BaseStats.RARITY_LIST)
item_rarity_sizer.AddSpacer(7)
item_rarity_sizer.Add(item_rarity_label, 0, wx.ALIGN_CENTER_HORIZONTAL)
item_rarity_sizer.AddSpacer(5)
item_rarity_sizer.Add(item_rarity_cbox, wx.EXPAND)
top_sizer.Add(item_rarity_sizer, 0, wx.LEFT | wx.RIGHT, border=5)
top_panel.SetSizer(top_sizer)
top_panel.Layout()
# =============================== Mods ==================================
mod_panel = wx.Panel(main_panel, pos=(10, 200))
mod_panel.SetSize(470, 70)
mod_panel.SetBackgroundColour('#6b756c')
# mod_sizer = wx.BoxSizer(wx.VERTICAL)
mod_intermediate_sizer = wx.BoxSizer(wx.HORIZONTAL)
# Коробка с первым модом
first_mod_sizer = wx.BoxSizer(wx.VERTICAL)
first_mod_label = wx.StaticText(mod_panel, label="First mod")
first_mod_label.SetFont(sublabel_font)
first_mod_cbox = wx.ComboBox(mod_panel, value=self.current_item.mod_list[0].key, choices=mod_list)
first_mod_sizer.AddSpacer(7)
first_mod_sizer.Add(first_mod_label, 0, flag=wx.ALIGN_CENTER_HORIZONTAL)
first_mod_sizer.AddSpacer(20)
first_mod_sizer.Add(first_mod_cbox, flag=wx.EXPAND)
mod_intermediate_sizer.Add(first_mod_sizer, 0, wx.LEFT | wx.RIGHT, border=5)
# Коробка со вторым модом
second_mod_sizer = wx.BoxSizer(wx.VERTICAL)
second_mod_label = wx.StaticText(mod_panel, label='Second mod')
second_mod_label.SetFont(sublabel_font)
second_mod_cbox = wx.ComboBox(mod_panel, value=self.current_item.mod_list[1].key,
choices=mod_list)
second_mod_sizer.AddSpacer(7)
second_mod_sizer.Add(second_mod_label, 0, flag=wx.ALIGN_CENTER_HORIZONTAL)
second_mod_sizer.AddSpacer(5)
second_mod_sizer.Add(second_mod_cbox, 0, flag=wx.EXPAND)
mod_intermediate_sizer.Add(second_mod_sizer, 0, flag=wx.LEFT | wx.RIGHT, border=5)
mod_panel.SetSizer(mod_intermediate_sizer)
mod_intermediate_sizer.Layout()
'''
答案 0 :(得分:1)
不应调用sizer的Layout而不是面板。但是,嘿,发布问题后如何找到答案自己很有趣+)