wx.Panel wx.Python 中未显示

wx.Panel not showing in wx.Python

我开始使用 wxpython,并且正在尝试做一个小视频游戏,有点像俄罗斯方块。我试图在 GridGagSizer 中使用面板显示块,但面板不显示。这是我的代码:

import wx

altura=12
class MenuFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: CalculatorFrame.__init__
        #kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.SetTitle("Deslizador")
        self.Center()

        self.half_sizer=wx.SplitterWindow(self,wx.ID_ANY)
        self.settings=wx.Panel(self.half_sizer)
        self.grid_side=wx.Panel(self.half_sizer)
        #self.grid_side.SetBackgroundColour("red")
        self.half_sizer.SetMinimumPaneSize(130)
        #self.half_sizer.SetSashPosition(100)
        self.half_sizer.SplitVertically(self.settings,self.grid_side)

        self.options_separator=wx.BoxSizer(wx.VERTICAL)
        
        self.fichero_separator=wx.BoxSizer(wx.HORIZONTAL)

        self.label_fichero=wx.StaticText(self.settings,wx.ID_ANY,"Fichero: ")
        self.fichero_separator.Add(self.label_fichero,0)

        self.control_fichero=wx.TextCtrl(self.settings,wx.ID_ANY)
        self.fichero_separator.Add(self.control_fichero,1,wx.EXPAND)

        self.options_separator.Add(self.fichero_separator,0,wx.EXPAND)

        self.abr_fich=wx.Button(self.settings,wx.ID_ANY,"Abrir fichero")
        self.options_separator.Add(self.abr_fich,0,wx.EXPAND)

        self.nuev_part = wx.Button(self.settings, wx.ID_ANY, "Nueva partida")
        self.options_separator.Add(self.nuev_part, 0, wx.EXPAND)

        self.sep_n_filas=wx.BoxSizer(wx.HORIZONTAL)

        self.label_n_filas=wx.StaticText(self.settings,wx.ID_ANY,"N° Filas:")
        self.sep_n_filas.Add(self.label_n_filas,0,wx.ALIGN_CENTER_VERTICAL)

        self.spin_n_filas = wx.SpinCtrl(self.settings, wx.ID_ANY,min=3,max=100,initial=12)
        self.sep_n_filas.Add(self.spin_n_filas, 1, wx.EXPAND)

        self.options_separator.Add(self.sep_n_filas,0,wx.EXPAND)

        self.sep_jugadas=wx.BoxSizer(wx.HORIZONTAL)

        self.label_jugada = wx.StaticText(self.settings, wx.ID_ANY, "Jugada: ")
        self.sep_jugadas.Add(self.label_jugada, 0,wx.ALIGN_CENTER_VERTICAL)

        self.control_jugada = wx.TextCtrl(self.settings, wx.ID_ANY)
        self.sep_jugadas.Add(self.control_jugada, 1, wx.EXPAND)

        self.options_separator.Add(self.sep_jugadas,0,wx.EXPAND)


        self.label_lista = wx.StaticText(self.settings, wx.ID_ANY, "Lista de jugadas:")
        self.options_separator.Add(self.label_lista, 0)

        self.lista = wx.ListBox(self.settings)
        self.options_separator.Add(self.lista, 10, wx.EXPAND)

        font = wx.Font(20, family=wx.FONTFAMILY_MODERN, style=0, weight=90,underline=False, faceName="", encoding=wx.FONTENCODING_DEFAULT)
        self.label_puntos = wx.StaticText(self.settings, wx.ID_ANY, "PTOS: 0")
        self.label_puntos.SetFont(font)
        self.options_separator.Add(self.label_puntos, 0,wx.CENTER)

        self.settings.SetSizer(self.options_separator)

        self.tabla=wx.GridBagSizer(5,5)
        panel_add = wx.Panel()
        # panel_add=wx.StaticText(parent.grid_side, wx.ID_ANY,"test")
        panel_add.SetBackgroundColour("red")
        self.tabla.Add(panel_add, wx.GBPosition(3,3), wx.GBSpan(1, 3), flag=wx.EXPAND)
        print(self.tabla.FindItemAtPosition(wx.GBPosition(3,3)))
        self.draw_table([[3,3,"E",3]],self)


        self.grid_side.SetSizer(self.tabla)
        self.Layout()
    def draw_table(self,blocks,parent):
        for i in range(12):
            text_add = wx.StaticText(parent.grid_side, wx.ID_ANY,"abcdefghijklmopqrstuvwxyz"[i])
            parent.tabla.Add(text_add, wx.GBPosition(i, 0), wx.GBSpan(1, 1))
        for i in range(10):
            text_add = wx.StaticText(parent.grid_side, wx.ID_ANY, str(i))
            parent.tabla.Add(text_add, wx.GBPosition(12, i), wx.GBSpan(1, 1))
        """for i in blocks:
            panel_add = wx.Panel()
            # panel_add=wx.StaticText(parent.grid_side, wx.ID_ANY,"test")
            panel_add.SetBackgroundColour("red")
            parent.tabla.Add(panel_add, wx.GBPosition(i[0], i[3]), wx.GBSpan(1, 3), flag=wx.EXPAND)
            print(i[0],i[3])"""
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MenuFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()

如果你 运行 它,你会注意到静态文本显示完美,但面板没有。我什至使用打印行来检查它是否已正确添加到 GridBagSizer 中,是的,是的。 谁能帮我? PS: 这可能是我犯的一个非常愚蠢的错误,我从 wxpython 开始。

我相信你的问题出在这里:panel_add = wx.Panel(),它没有 parent
我怀疑以下内容将解决您当前的问题(我已将其重命名为 block 并将其定义为 block = wx.Panel(self.grid_side)):

import wx

altura=12
class MenuFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: CalculatorFrame.__init__
        #kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.SetTitle("Deslizador")
        self.Center()

        self.half_sizer=wx.SplitterWindow(self,wx.ID_ANY)
        self.settings=wx.Panel(self.half_sizer)
        self.grid_side=wx.Panel(self.half_sizer)
        #self.grid_side.SetBackgroundColour("red")
        self.half_sizer.SetMinimumPaneSize(130)
        #self.half_sizer.SetSashPosition(100)
        self.half_sizer.SplitVertically(self.settings,self.grid_side)

        self.options_separator=wx.BoxSizer(wx.VERTICAL)
        
        self.fichero_separator=wx.BoxSizer(wx.HORIZONTAL)

        self.label_fichero=wx.StaticText(self.settings,wx.ID_ANY,"Fichero: ")
        self.fichero_separator.Add(self.label_fichero,0)

        self.control_fichero=wx.TextCtrl(self.settings,wx.ID_ANY)
        self.fichero_separator.Add(self.control_fichero,1,wx.EXPAND)

        self.options_separator.Add(self.fichero_separator,0,wx.EXPAND)

        self.abr_fich=wx.Button(self.settings,wx.ID_ANY,"Abrir fichero")
        self.options_separator.Add(self.abr_fich,0,wx.EXPAND)

        self.nuev_part = wx.Button(self.settings, wx.ID_ANY, "Nueva partida")
        self.options_separator.Add(self.nuev_part, 0, wx.EXPAND)

        self.sep_n_filas=wx.BoxSizer(wx.HORIZONTAL)

        self.label_n_filas=wx.StaticText(self.settings,wx.ID_ANY,"N° Filas:")
        self.sep_n_filas.Add(self.label_n_filas,0,wx.ALIGN_CENTER_VERTICAL)

        self.spin_n_filas = wx.SpinCtrl(self.settings, wx.ID_ANY,min=3,max=100,initial=12)
        self.sep_n_filas.Add(self.spin_n_filas, 1, wx.EXPAND)

        self.options_separator.Add(self.sep_n_filas,0,wx.EXPAND)

        self.sep_jugadas=wx.BoxSizer(wx.HORIZONTAL)

        self.label_jugada = wx.StaticText(self.settings, wx.ID_ANY, "Jugada: ")
        self.sep_jugadas.Add(self.label_jugada, 0,wx.ALIGN_CENTER_VERTICAL)

        self.control_jugada = wx.TextCtrl(self.settings, wx.ID_ANY)
        self.sep_jugadas.Add(self.control_jugada, 1, wx.EXPAND)

        self.options_separator.Add(self.sep_jugadas,0,wx.EXPAND)


        self.label_lista = wx.StaticText(self.settings, wx.ID_ANY, "Lista de jugadas:")
        self.options_separator.Add(self.label_lista, 0)

        self.lista = wx.ListBox(self.settings)
        self.options_separator.Add(self.lista, 10, wx.EXPAND)

        font = wx.Font(20, family=wx.FONTFAMILY_MODERN, style=0, weight=90,underline=False, faceName="", encoding=wx.FONTENCODING_DEFAULT)
        self.label_puntos = wx.StaticText(self.settings, wx.ID_ANY, "PTOS: 0")
        self.label_puntos.SetFont(font)
        self.options_separator.Add(self.label_puntos, 0,wx.CENTER)

        self.settings.SetSizer(self.options_separator)

        self.tabla=wx.GridBagSizer(5,5)
 

        self.draw_table(self)
        self.draw_blocks([[3,3,"E",3]],self)
        self.draw_blocks([[4,3,"E",3]],self)
        self.draw_blocks([[1,3,"E",8]],self)
        self.draw_blocks([[8,3,"E",1]],self)
        self.draw_blocks([[8,3,"E",2]],self)
        self.draw_blocks([[7,3,"A",5],[7,3,"A",7],[7,3,"A",8]],self)

        self.grid_side.SetSizer(self.tabla)
        self.Layout()
        self.Show()
 
    def draw_table(self,parent):
        for i in range(12):
            text_add = wx.StaticText(parent.grid_side, wx.ID_ANY,"abcdefghijklmopqrstuvwxyz"[i])
            parent.tabla.Add(text_add, wx.GBPosition(i, 0), wx.GBSpan(1, 1))
        for i in range(10):
            text_add = wx.StaticText(parent.grid_side, wx.ID_ANY, str(i))
            parent.tabla.Add(text_add, wx.GBPosition(12, i), wx.GBSpan(1, 1))

    def draw_blocks(self,blocks,parent):
        for i in blocks:
            block = wx.Panel(self.grid_side)
            block.SetBackgroundColour("red")
            parent.tabla.Add(block, wx.GBPosition(i[0], i[3]), flag=wx.EXPAND)


if __name__ == "__main__":
    app = wx.App()
    MenuFrame(None, wx.ID_ANY, "")
    app.MainLoop()

显然,这些块是随机测试。