如何解决wx.Panel 未在 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,**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,wx.EXPAND)
self.abr_fich=wx.Button(self.settings,"Abrir fichero")
self.options_separator.Add(self.abr_fich,wx.EXPAND)
self.nuev_part = wx.Button(self.settings,"Nueva partida")
self.options_separator.Add(self.nuev_part,wx.EXPAND)
self.sep_n_filas=wx.BoxSizer(wx.HORIZONTAL)
self.label_n_filas=wx.StaticText(self.settings,"N° Filas:")
self.sep_n_filas.Add(self.label_n_filas,wx.ALIGN_CENTER_VERTICAL)
self.spin_n_filas = wx.SpinCtrl(self.settings,min=3,max=100,initial=12)
self.sep_n_filas.Add(self.spin_n_filas,wx.EXPAND)
self.options_separator.Add(self.sep_n_filas,wx.EXPAND)
self.sep_jugadas=wx.BoxSizer(wx.HORIZONTAL)
self.label_jugada = wx.StaticText(self.settings,"Jugada: ")
self.sep_jugadas.Add(self.label_jugada,wx.ALIGN_CENTER_VERTICAL)
self.control_jugada = wx.TextCtrl(self.settings,wx.ID_ANY)
self.sep_jugadas.Add(self.control_jugada,wx.EXPAND)
self.options_separator.Add(self.sep_jugadas,wx.EXPAND)
self.label_lista = wx.StaticText(self.settings,"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,"PTOS: 0")
self.label_puntos.SetFont(font)
self.options_separator.Add(self.label_puntos,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,"test")
panel_add.SetBackgroundColour("red")
self.tabla.Add(panel_add,wx.GBPosition(3,3),wx.GBSpan(1,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,"abcdefghijklmopqrstuvwxyz"[i])
parent.tabla.Add(text_add,wx.GBPosition(i,0),1))
for i in range(10):
text_add = wx.StaticText(parent.grid_side,str(i))
parent.tabla.Add(text_add,wx.GBPosition(12,i),1))
"""for i in blocks:
panel_add = wx.Panel()
# panel_add=wx.StaticText(parent.grid_side,"test")
panel_add.SetBackgroundColour("red")
parent.tabla.Add(panel_add,wx.GBPosition(i[0],i[3]),flag=wx.EXPAND)
print(i[0],i[3])"""
class MyApp(wx.App):
def OnInit(self):
self.frame = MenuFrame(None,"")
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,**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,wx.EXPAND)
self.abr_fich=wx.Button(self.settings,"Abrir fichero")
self.options_separator.Add(self.abr_fich,wx.EXPAND)
self.nuev_part = wx.Button(self.settings,"Nueva partida")
self.options_separator.Add(self.nuev_part,wx.EXPAND)
self.sep_n_filas=wx.BoxSizer(wx.HORIZONTAL)
self.label_n_filas=wx.StaticText(self.settings,"N° Filas:")
self.sep_n_filas.Add(self.label_n_filas,wx.ALIGN_CENTER_VERTICAL)
self.spin_n_filas = wx.SpinCtrl(self.settings,min=3,max=100,initial=12)
self.sep_n_filas.Add(self.spin_n_filas,wx.EXPAND)
self.options_separator.Add(self.sep_n_filas,wx.EXPAND)
self.sep_jugadas=wx.BoxSizer(wx.HORIZONTAL)
self.label_jugada = wx.StaticText(self.settings,"Jugada: ")
self.sep_jugadas.Add(self.label_jugada,wx.ALIGN_CENTER_VERTICAL)
self.control_jugada = wx.TextCtrl(self.settings,wx.ID_ANY)
self.sep_jugadas.Add(self.control_jugada,wx.EXPAND)
self.options_separator.Add(self.sep_jugadas,wx.EXPAND)
self.label_lista = wx.StaticText(self.settings,"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,"PTOS: 0")
self.label_puntos.SetFont(font)
self.options_separator.Add(self.label_puntos,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,self)
self.draw_blocks([[1,8]],self)
self.draw_blocks([[8,1]],2]],self)
self.draw_blocks([[7,"A",5],[7,7],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,"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,str(i))
parent.tabla.Add(text_add,wx.GBPosition(12,i),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,"")
app.MainLoop()
显然,这些块是随机测试。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。