wx.Panel 缩放以适应整个父 Frame 尽管给它一个大小 [英] wx.Panel scales to fit entire parent Frame despite giving it a size

查看:33
本文介绍了wx.Panel 缩放以适应整个父 Frame 尽管给它一个大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 wxpython 的新手.我试图有一个框架,并在我将其着色为蓝色的小面板区域内.但是,无论我使用 size 属性对 wx.Panel 做什么,单个面板都会捕捉到其父框架的大小.如果我添加另一个面板(下面代码中的面板 2),两个面板都以正确的大小绘制.

I am a newbie to wxpython. I am trying to have a Frame and within that a small panel area which I am coloring blue. However no matter what I do the wx.Panel using the size attribute, the single panel snaps to the size of its parent frame. If I add another panel (pane2 in code below) both panes are drawn in the correct size.

我知道我可以使用 sizer 控制这些面板.但我试图理解为什么 wx.Panel 对象在它独自一人时的行为方式.

I know I can control these panels using sizers. But I was trying to understand why the wx.Panel bject behaves the way it does when it's all alone.

代码如下:

import wx

    class PlateGui(wx.Frame):

    def __init__(self, *args , **kwds):
        self.frame = wx.Frame.__init__(self,*args, **kwds)
        print "Made frame"


if __name__ == "__main__":
    an_app = wx.PySimpleApp()
    aframe = PlateGui(parent=None,id=-1,title="Test Frame",size=(300, 300))
    pane = wx.Panel(parent=aframe,size=(100,100),style=wx.RAISED_BORDER)
    pane.SetBackgroundColour(wx.Colour(0,0,255))
 #  pane2 = wx.Panel(parent=aframe,size=(200,100),style=wx.RAISED_BORDER)
 # Commenting out the second pane makes the first pane fit 
 # entire frame regardless of size specified  
    aframe.Show()
    an_app.MainLoop()

推荐答案

默认情况下,wx.Frame 有一个 sizer,可以扩展其子项以填充框架.创建您自己的 sizer,向其中添加面板(不指定展开标志)并将其设置为框架的 sizer.

By default, wx.Frame has a sizer that expands its child to fill the frame. Create your own sizer, add the panel to it (without specifying expand flags) and set that as the frame's sizer.

import wx
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, 'Test')
sizer = wx.BoxSizer(wx.VERTICAL)
panel = wx.Panel(frame, -1, size=(100,100), style=wx.BORDER_RAISED)
sizer.Add(panel)
frame.SetSizer(sizer)
frame.Show()
app.MainLoop()

这篇关于wx.Panel 缩放以适应整个父 Frame 尽管给它一个大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆