如何解决在顶部和中间放置一个盒子
我正在尝试在网格中放置四个蓝色框。首先,我想从中间和顶部的一个蓝色框开始。不幸的是,我什至无法找出一个盒子来正确放置它。 我试着做两个盒子和一个面板。我试过使用静态文本并且它有效,所以我想用盒子调整这个原则,但我不知道我做错了什么。你能看看我做错了什么吗?
我想要实现的是:
下面是我的代码:
import wx
class Panel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent)
panel1 = self.panels()
self.Boxes(panel1)
def panels(self):
panel1 = wx.Panel(self)
panel1.SetBackgroundColour([0,178,202])
return panel1
def Boxes(self,panel1):
hblueBox1 = wx.BoxSizer()
vblueBox1 = wx.BoxSizer(wx.VERTICAL)
hblueBox1.Add(panel1,6,wx.ALIGN_TOP)
vblueBox1.Add(hblueBox1,wx.ALIGN_CENTRE)
self.SetSizer(vblueBox1)
return vblueBox1
if __name__ == '__main__':
class Screen(wx.Frame):
def __init__(self,parent,id,title):
wx.Frame.__init__(self,title,size=(600,400))
frameBox = wx.BoxSizer()
panel = Panel(self)
frameBox.Add(panel,1,wx.EXPAND | wx.ALL)
self.SetSizer(frameBox)
self.Show()
app = wx.App()
Screen(None,-1,"Kleuruitdager 2")
app.MainLoop()
解决方法
您可以使用简单的彩色面板和 wx.GridBagSizer
我会让你完成调整和中央面板(也许以类似的方式实现)。
当然,另一种选择是按照评论中的建议开始绘图。
import wx
class Panel(wx.Panel):
def __init__(self,parent):
wx.Panel.__init__(self,parent)
sizer = wx.GridBagSizer(3,3)
top_lpanel = wx.Panel(self,wx.ID_ANY,size=(100,100))
top_mpanel = wx.Panel(self,100))
top_rpanel = wx.Panel(self,100))
top_lpanel.SetBackgroundColour("white")
top_mpanel.SetBackgroundColour("blue")
top_rpanel.SetBackgroundColour("white")
mid_lpanel = wx.Panel(self,100))
mid_mpanel = wx.Panel(self,100))
mid_rpanel = wx.Panel(self,100))
mid_lpanel.SetBackgroundColour("blue")
mid_mpanel.SetBackgroundColour("white")
mid_rpanel.SetBackgroundColour("blue")
bot_lpanel = wx.Panel(self,100))
bot_mpanel = wx.Panel(self,100))
bot_rpanel = wx.Panel(self,100))
bot_lpanel.SetBackgroundColour("white")
bot_mpanel.SetBackgroundColour("blue")
bot_rpanel.SetBackgroundColour("white")
sizer.Add(top_lpanel,pos=(0,0),flag=wx.EXPAND,border = 10)
sizer.Add(top_mpanel,1),border = 10)
sizer.Add(top_rpanel,2),border = 10)
sizer.Add(mid_lpanel,pos=(1,border = 10)
sizer.Add(mid_mpanel,border = 10)
sizer.Add(mid_rpanel,border = 10)
sizer.Add(bot_lpanel,pos=(2,border = 10)
sizer.Add(bot_mpanel,border = 10)
sizer.Add(bot_rpanel,border = 10)
self.SetSizer(sizer)
if __name__ == '__main__':
class Scherm(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent)
framebox = wx.BoxSizer()
panel = Panel(self)
framebox.Add(panel,1,wx.EXPAND | wx.ALL)
self.SetSizerAndFit(framebox)
self.Show()
app = wx.App()
Scherm(None)
app.MainLoop()
,
谢谢大家的帮助,今天终于知道怎么做了。现在我明白了如何做到这一点。 Endresult
import wx
class Panel(wx.Panel):
def __init__(self,parent)
#blue and non-colored boxes
left_panel1 = self.box1()
mid_panel1,white_box = self.box2()
right_panel1 = self.box3()
#yellow_boxes
mid_yellow_panel1 = self.yellow_top1()
mid_yellow_panel2 = self.yellow_top2()
mid_yellow_panel3 = self.yellow_top3()
#box for the yellow boxes
self.yellow_boxes(white_box,mid_yellow_panel1,mid_yellow_panel2,mid_yellow_panel3)
#The mainbox
main_box = self.boxes(left_panel1,mid_panel1,right_panel1)
self.SetSizer(main_box)
def box1(self):
left_panel1 = wx.BoxSizer(wx.VERTICAL)
# 1e witte box
white_top1 = wx.Panel(self)
# 1e witte box in de grote box
left_panel1.Add(white_top1,7,wx.EXPAND | wx.ALL)
# 1e blauwe box
blue_top1 = wx.Panel(self)
blue_top1.SetBackgroundColour([0,178,202])
# Voegt 1e blauwe box toe in de grote box
left_panel1.Add(blue_top1,6,wx.EXPAND | wx.ALL)
# 2e witte box
white_bottom1 = wx.Panel(self)
# 2e witte box in de grote box
left_panel1.Add(white_bottom1,wx.EXPAND | wx.ALL)
return left_panel1
def box2(self):
mid_panel1 = wx.BoxSizer(wx.VERTICAL)
# 1e blauwe box
blue_top1 = wx.Panel(self)
blue_top1.SetBackgroundColour([0,202])
# Voegt 1e blauwe box toe in de grote box
mid_panel1.Add(blue_top1,wx.EXPAND | wx.ALL)
# 1e witte box
white_box = wx.BoxSizer()
# 1e witte box in de grote box
mid_panel1.Add(white_box,wx.EXPAND | wx.ALL)
# 2e blauwe box
blue_top1 = wx.Panel(self)
blue_top1.SetBackgroundColour([0,wx.EXPAND | wx.ALL)
return mid_panel1,white_box
def box3(self):
right_panel1 = wx.BoxSizer(wx.VERTICAL)
# 1e witte box
white_top1 = wx.Panel(self)
# 1e witte box in de grote box
right_panel1.Add(white_top1,202])
# Voegt 1e blauwe box toe in de grote box
right_panel1.Add(blue_top1,wx.EXPAND | wx.ALL)
# 2e witte box
white_bottom1 = wx.Panel(self)
# 2e witte box in de grote box
right_panel1.Add(white_bottom1,wx.EXPAND | wx.ALL)
return right_panel1
#Yellow panels
def yellow_top1(self):
mid_yellow_panel1 = wx.BoxSizer(wx.VERTICAL)
# 1e witte box
white_top1 = wx.Panel(self)
# 1e witte box in de grote box
mid_yellow_panel1.Add(white_top1,3,wx.EXPAND | wx.ALL)
# 1e gele box
yellow_top = wx.Panel(self)
yellow_top.SetBackgroundColour(wx.YELLOW)
# Voegt 1e gele box toe in de grote box
mid_yellow_panel1.Add(yellow_top,2,wx.EXPAND | wx.ALL)
# 2e witte box
white_bottom1 = wx.Panel(self)
# 2e witte box in de grote box
mid_yellow_panel1.Add(white_bottom1,wx.EXPAND | wx.ALL)
return mid_yellow_panel1
def yellow_top2(self):
mid_yellow_panel2 = wx.BoxSizer(wx.VERTICAL)
# 1e gele box
yellow_top = wx.Panel(self)
yellow_top.SetBackgroundColour(wx.YELLOW)
# Voegt 1e gele box toe in de grote box
mid_yellow_panel2.Add(yellow_top,wx.EXPAND | wx.ALL)
# 1e witte box
white_top1 = wx.Panel(self)
# 1e witte box in de grote box
mid_yellow_panel2.Add(white_top1,4,wx.EXPAND | wx.ALL)
# 2e gele box
yellow_top = wx.Panel(self)
yellow_top.SetBackgroundColour(wx.YELLOW)
# Voegt 1e gele box toe in de grote box
mid_yellow_panel2.Add(yellow_top,wx.EXPAND | wx.ALL)
return mid_yellow_panel2
def yellow_top3(self):
mid_yellow_panel3 = wx.BoxSizer(wx.VERTICAL)
# 1e witte box
white_top1 = wx.Panel(self)
# 1e witte box in de grote box
mid_yellow_panel3.Add(white_top1,wx.EXPAND | wx.ALL)
# 1e gele box
yellow_top = wx.Panel(self)
yellow_top.SetBackgroundColour(wx.YELLOW)
# Voegt 1e gele box toe in de grote box
mid_yellow_panel3.Add(yellow_top,wx.EXPAND | wx.ALL)
# 2e witte box
white_bottom1 = wx.Panel(self)
# 2e witte box in de grote box
mid_yellow_panel3.Add(white_bottom1,wx.EXPAND | wx.ALL)
return mid_yellow_panel3
def yellow_boxes(self,white_box,mid_yellow_panel3):
white_box.Add(mid_yellow_panel1,8,wx.EXPAND | wx.ALL)
white_box.Add(mid_yellow_panel2,wx.EXPAND | wx.ALL)
white_box.Add(mid_yellow_panel3,wx.EXPAND | wx.ALL)
return white_box
def boxes(self,left_panel1,right_panel1):
main_box = wx.BoxSizer()
main_box.Add(left_panel1,20,wx.EXPAND | wx.ALL)
main_box.Add(mid_panel1,wx.EXPAND | wx.ALL)
main_box.Add(right_panel1,wx.EXPAND | wx.ALL)
return main_box
if __name__ == '__main__':
class Scherm(wx.Frame):
def __init__(self,parent,size1):
wx.Frame.__init__(self,size = size1)
framebox = wx.BoxSizer()
panel = Panel(self)
framebox.Add(panel,wx.EXPAND | wx.ALL)
self.SetSizer(framebox)
self.Show()
app = wx.App()
Scherm(None,(600,600))
app.MainLoop()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。