Python论坛  - 讨论区

标题:[python-chinese] 请教一个wxPython窗口布局的问题

2007年02月06日 星期二 11:39

amingsc amingsc在gmail.com
星期二 二月 6 11:39:28 HKT 2007

下面的代码是在一个窗口中添加四个不同颜色的子窗口,子窗口的width随着父窗
口缩放,
怎样让子窗口的高度height保持跟width一定的比例进行缩放呢?比如我们限定子
窗口必须是正方形;
请问各位这怎么实现比较好呢?thanku

import wx

class SubWindow(wx.Panel):
def __init__(self,parent,colour=None):
wx.Panel.__init__(self,parent,id=-1)
self.SetBackgroundColour(colour) #设置子窗口的背景颜色

class ParentWindow(wx.Frame):
def __init__(self,parent,id=-1,size=None):
wx.Frame.__init__(self,parent,id=id,size=size)
sz = wx.BoxSizer(wx.VERTICAL)

colours = ('red','black','yellow','blue')
subs = [SubWindow(self,colour=i) for i in colours] #构造四个不同颜色的子窗口
for w in subs: sz.Add(w,0,wx.EXPAND|wx.ALL,5) #将四个子窗口进行布局

self.SetSizer(sz)
self.SetAutoLayout(True)

if __name__ == '__main__':
class MyApp(wx.App):
def OnInit(self):
frame = ParentWindow(None, id=-1,size=(200,600))
frame.Show(True)
self.SetTopWindow(frame)
return True

app = MyApp(False)
app.MainLoop()

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年02月06日 星期二 11:48

spring lee ncepu.lyc在gmail.com
星期二 二月 6 11:48:19 HKT 2007

try using style wxSHAPED

On 06/02/07, amingsc <amingsc at gmail.com> wrote:
> 下面的代码是在一个窗口中添加四个不同颜色的子窗口,子窗口的width随着父窗
> 口缩放,
> 怎样让子窗口的高度height保持跟width一定的比例进行缩放呢?比如我们限定子
> 窗口必须是正方形;
> 请问各位这怎么实现比较好呢?thanku

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年02月06日 星期二 11:48

刘鑫 march.liu在gmail.com
星期二 二月 6 11:48:47 HKT 2007

ÎÒ¾õµÃSizer×ö²»µ½£¬Äã¿ÖÅÂÒª×Ô¼ºÐ´OnResizeʼþ

2007/2/6, amingsc <amingsc在gmail.com>:
>
> ÏÂÃæµÄ´úÂëÊÇÔÚÒ»¸ö´°¿ÚÖÐÌí¼ÓËĸö²»Í¬ÑÕÉ«µÄ×Ó´°¿Ú£¬×Ó´°¿ÚµÄwidthËæן¸´°
> ¿ÚËõ·Å£¬
> ÔõÑùÈÃ×Ó´°¿ÚµÄ¸ß¶Èheight±£³Ö¸úwidthÒ»¶¨µÄ±ÈÀý½øÐÐËõ·ÅÄØ£¿±ÈÈçÎÒÃÇÏÞ¶¨×Ó
> ´°¿Ú±ØÐëÊÇÕý·½ÐΣ»
> ÇëÎʸ÷λÕâÔõôʵÏֱȽϺÃÄØ£¿thanku
>
> import wx
>
> class SubWindow(wx.Panel):
> def __init__(self,parent,colour=None):
> wx.Panel.__init__(self,parent,id=-1)
> self.SetBackgroundColour(colour) #ÉèÖÃ×Ó´°¿ÚµÄ±³¾°ÑÕÉ«
>
> class ParentWindow(wx.Frame):
> def __init__(self,parent,id=-1,size=None):
> wx.Frame.__init__(self,parent,id=id,size=size)
> sz = wx.BoxSizer(wx.VERTICAL)
>
> colours = ('red','black','yellow','blue')
> subs = [SubWindow(self,colour=i) for i in colours] #¹¹ÔìËĸö²»Í¬ÑÕÉ«µÄ×Ó´°¿Ú
> for w in subs: sz.Add(w,0,wx.EXPAND|wx.ALL,5) #½«Ëĸö×Ó´°¿Ú½øÐв¼¾Ö
>
> self.SetSizer(sz)
> self.SetAutoLayout(True)
>
> if __name__ == '__main__':
> class MyApp(wx.App):
> def OnInit(self):
> frame = ParentWindow(None, id=-1,size=(200,600))
> frame.Show(True)
> self.SetTopWindow(frame)
> return True
>
> app = MyApp(False)
> app.MainLoop()
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Blog°á¼ÒÁË

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070206/40acb6da/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年02月06日 星期二 12:03

amingsc amingsc在gmail.com
星期二 二月 6 12:03:53 HKT 2007

对,我跟你想的一样,但是有个问题是子窗口的大小是改变了,但布局还是有问
题,就是说父窗口仍然是按修改前的高度进行布局的,所以后面的子窗口就将前面
的子窗口给掩盖了一部分
下面就是处理wx.EVT_SIZE事件的函数,即使强制父窗口重新布局也不行,不知道
应该怎样让父窗口按新的大小进行布局?
def OnSize(self, event):
size = event.GetSize()
self.SetClientSizeWH(size.width,size.width)


刘鑫 写道:
> 我觉得Sizer做不到,你恐怕要自己写OnResize事件
>
> 2007/2/6, amingsc <amingsc at gmail.com amingsc at gmail.com>>:
>
>     下面的代码是在一个窗口中添加四个不同颜色的子窗口,子窗口的width随
>     着父窗
>     口缩放,
>     怎样让子窗口的高度height保持跟width一定的比例进行缩放呢?比如我们
>     限定子
>     窗口必须是正方形;
>     请问各位这怎么实现比较好呢?thanku
>
>     import wx
>
>     class SubWindow(wx.Panel):
>     def __init__(self,parent,colour=None):
>     wx.Panel.__init__(self,parent,id=-1)
>     self.SetBackgroundColour(colour) #设置子窗口的背景颜色
>
>     class ParentWindow(wx.Frame):
>     def __init__(self,parent,id=-1,size=None):
>     wx.Frame.__init__(self,parent,id=id,size=size)
>     sz = wx.BoxSizer(wx.VERTICAL)
>
>     colours = ('red','black','yellow','blue')
>     subs = [SubWindow(self,colour=i) for i in colours] #构造四个不同颜
>     色的子窗口
>     for w in subs: sz.Add(w,0,wx.EXPAND|wx.ALL ,5) #将四个子窗口进行布局
>
>     self.SetSizer(sz)
>     self.SetAutoLayout(True)
>
>     if __name__ == '__main__':
>     class MyApp(wx.App):
>     def OnInit(self):
>     frame = ParentWindow(None, id=-1,size=(200,600))
>     frame.Show (True)
>     self.SetTopWindow(frame)
>     return True
>
>     app = MyApp(False)
>     app.MainLoop()
>     _______________________________________________
>     python-chinese
>     Post: send python-chinese at lists.python.cn
>     python-chinese at lists.python.cn>
>     Subscribe: send subscribe to
>     python-chinese-request at lists.python.cn
>     python-chinese-request at lists.python.cn>
>     Unsubscribe: send unsubscribe to
>     python-chinese-request at lists.python.cn
>     python-chinese-request at lists.python.cn>
>     Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>
>
>
> -- 
> Blog搬家了
>
> 刘鑫
> March.Liu
> ------------------------------------------------------------------------
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年02月06日 星期二 12:43

amingsc amingsc在gmail.com
星期二 二月 6 12:43:08 HKT 2007

终于搞定了,用SetMinSize和SetMaxSize就可以了
def OnSize(self, event):
________size = event.GetSize()
________self.SetMinSize(wx.Size(size.width,size.width))
________self.SetMaxSize(wx.Size(size.width,size.width))
________self.parent.Layout()

amingsc 写道:
> 对,我跟你想的一样,但是有个问题是子窗口的大小是改变了,但布局还是有问
> 题,就是说父窗口仍然是按修改前的高度进行布局的,所以后面的子窗口就将前面
> 的子窗口给掩盖了一部分
> 下面就是处理wx.EVT_SIZE事件的函数,即使强制父窗口重新布局也不行,不知道
> 应该怎样让父窗口按新的大小进行布局?
> def OnSize(self, event):
> size = event.GetSize()
> self.SetClientSizeWH(size.width,size.width)
>
>
> 刘鑫 写道:
>   
>> 我觉得Sizer做不到,你恐怕要自己写OnResize事件
>>
>> 2007/2/6, amingsc <amingsc at gmail.com amingsc at gmail.com>>:
>>
>>     下面的代码是在一个窗口中添加四个不同颜色的子窗口,子窗口的width随
>>     着父窗
>>     口缩放,
>>     怎样让子窗口的高度height保持跟width一定的比例进行缩放呢?比如我们
>>     限定子
>>     窗口必须是正方形;
>>     请问各位这怎么实现比较好呢?thanku
>>
>>     import wx
>>
>>     class SubWindow(wx.Panel):
>>     def __init__(self,parent,colour=None):
>>     wx.Panel.__init__(self,parent,id=-1)
>>     self.SetBackgroundColour(colour) #设置子窗口的背景颜色
>>
>>     class ParentWindow(wx.Frame):
>>     def __init__(self,parent,id=-1,size=None):
>>     wx.Frame.__init__(self,parent,id=id,size=size)
>>     sz = wx.BoxSizer(wx.VERTICAL)
>>
>>     colours = ('red','black','yellow','blue')
>>     subs = [SubWindow(self,colour=i) for i in colours] #构造四个不同颜
>>     色的子窗口
>>     for w in subs: sz.Add(w,0,wx.EXPAND|wx.ALL ,5) #将四个子窗口进行布局
>>
>>     self.SetSizer(sz)
>>     self.SetAutoLayout(True)
>>
>>     if __name__ == '__main__':
>>     class MyApp(wx.App):
>>     def OnInit(self):
>>     frame = ParentWindow(None, id=-1,size=(200,600))
>>     frame.Show (True)
>>     self.SetTopWindow(frame)
>>     return True
>>
>>     app = MyApp(False)
>>     app.MainLoop()
>>     _______________________________________________
>>     python-chinese
>>     Post: send python-chinese at lists.python.cn
>>     python-chinese at lists.python.cn>
>>     Subscribe: send subscribe to
>>     python-chinese-request at lists.python.cn
>>     python-chinese-request at lists.python.cn>
>>     Unsubscribe: send unsubscribe to
>>     python-chinese-request at lists.python.cn
>>     python-chinese-request at lists.python.cn>
>>     Detail Info: http://python.cn/mailman/listinfo/python-chinese
>>
>>
>>
>>
>> -- 
>> Blog搬家了
>>
>> 刘鑫
>> March.Liu
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> python-chinese
>> Post: send python-chinese at lists.python.cn
>> Subscribe: send subscribe to python-chinese-request at lists.python.cn
>> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
>> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>>     
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年02月06日 星期二 15:05

刘鑫 march.liu在gmail.com
星期二 二月 6 15:05:41 HKT 2007

¹§Ï²£¬¸Ðл·ÖÏí

ÔÚ07-2-6£¬amingsc <amingsc在gmail.com> дµÀ£º
>
> ÖÕÓڸ㶨ÁË£¬ÓÃSetMinSizeºÍSetMaxSize¾Í¿ÉÒÔÁË
> def OnSize(self, event):
> ________size = event.GetSize()
> ________self.SetMinSize(wx.Size(size.width,size.width))
> ________self.SetMaxSize(wx.Size(size.width,size.width))
> ________self.parent.Layout()
>
> amingsc дµÀ:
> > ¶Ô£¬ÎÒ¸úÄãÏëµÄÒ»Ñù£¬µ«ÊÇÓиöÎÊÌâÊÇ×Ó´°¿ÚµÄ´óСÊǸıäÁË£¬µ«²¼¾Ö»¹ÊÇÓÐÎÊ
> > Ì⣬¾ÍÊÇ˵¸¸´°¿ÚÈÔÈ»ÊÇ°´ÐÞ¸ÄÇ°µÄ¸ß¶È½øÐв¼¾ÖµÄ£¬ËùÒÔºóÃæµÄ×Ó´°¿Ú¾Í½«Ç°Ãæ
> > µÄ×Ó´°¿Ú¸øÑÚ¸ÇÁËÒ»²¿·Ö
> > ÏÂÃæ¾ÍÊÇ´¦Àíwx.EVT_SIZEʼþµÄº¯Êý£¬¼´Ê¹Ç¿ÖƸ¸´°¿ÚÖØв¼¾ÖÒ²²»ÐУ¬²»ÖªµÀ
> > Ó¦¸ÃÔõÑùÈø¸´°¿Ú°´ÐµĴóС½øÐв¼¾Ö£¿
> > def OnSize(self, event):
> > size = event.GetSize()
> > self.SetClientSizeWH(size.width,size.width)
> >
> >
> > ÁõöΠдµÀ:
> >
> >> ÎÒ¾õµÃSizer×ö²»µ½£¬Äã¿ÖÅÂÒª×Ô¼ºÐ´OnResizeʼþ
> >>
> >> 2007/2/6, amingsc <amingsc在gmail.com amingsc在gmail.com>>:
> >>
> >>     ÏÂÃæµÄ´úÂëÊÇÔÚÒ»¸ö´°¿ÚÖÐÌí¼ÓËĸö²»Í¬ÑÕÉ«µÄ×Ó´°¿Ú£¬×Ó´°¿ÚµÄwidthËæ
> >>     ן¸´°
> >>     ¿ÚËõ·Å£¬
> >>     ÔõÑùÈÃ×Ó´°¿ÚµÄ¸ß¶Èheight±£³Ö¸úwidthÒ»¶¨µÄ±ÈÀý½øÐÐËõ·ÅÄØ£¿±ÈÈçÎÒÃÇ
> >>     ÏÞ¶¨×Ó
> >>     ´°¿Ú±ØÐëÊÇÕý·½ÐΣ»
> >>     ÇëÎʸ÷λÕâÔõôʵÏֱȽϺÃÄØ£¿thanku
> >>
> >>     import wx
> >>
> >>     class SubWindow(wx.Panel):
> >>     def __init__(self,parent,colour=None):
> >>     wx.Panel.__init__(self,parent,id=-1)
> >>     self.SetBackgroundColour(colour) #ÉèÖÃ×Ó´°¿ÚµÄ±³¾°ÑÕÉ«
> >>
> >>     class ParentWindow(wx.Frame):
> >>     def __init__(self,parent,id=-1,size=None):
> >>     wx.Frame.__init__(self,parent,id=id,size=size)
> >>     sz = wx.BoxSizer(wx.VERTICAL)
> >>
> >>     colours = ('red','black','yellow','blue')
> >>     subs = [SubWindow(self,colour=i) for i in colours] #¹¹ÔìËĸö²»Í¬ÑÕ
> >>     É«µÄ×Ó´°¿Ú
> >>     for w in subs: sz.Add(w,0,wx.EXPAND|wx.ALL ,5) #½«Ëĸö×Ó´°¿Ú½øÐв¼¾Ö
> >>
> >>     self.SetSizer(sz)
> >>     self.SetAutoLayout(True)
> >>
> >>     if __name__ == '__main__':
> >>     class MyApp(wx.App):
> >>     def OnInit(self):
> >>     frame = ParentWindow(None, id=-1,size=(200,600))
> >>     frame.Show (True)
> >>     self.SetTopWindow(frame)
> >>     return True
> >>
> >>     app = MyApp(False)
> >>     app.MainLoop()
> >>     _______________________________________________
> >>     python-chinese
> >>     Post: send python-chinese在lists.python.cn
> >>     python-chinese在lists.python.cn>
> >>     Subscribe: send subscribe to
> >>     python-chinese-request在lists.python.cn
> >>     python-chinese-request在lists.python.cn>
> >>     Unsubscribe: send unsubscribe to
> >>     python-chinese-request在lists.python.cn
> >>     python-chinese-request在lists.python.cn>
> >>     Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >>
> >>
> >>
> >>
> >> --
> >> Blog°á¼ÒÁË
> >>
> >> ÁõöÎ
> >> March.Liu
> >>
> ------------------------------------------------------------------------
> >>
> >> _______________________________________________
> >> python-chinese
> >> Post: send python-chinese在lists.python.cn
> >> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> >> Unsubscribe: send unsubscribe to
> python-chinese-request在lists.python.cn
> >> Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >>
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese在lists.python.cn
> > Subscribe: send subscribe to python-chinese-request在lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> _______________________________________________
> python-chinese
> Post: send python-chinese在lists.python.cn
> Subscribe: send subscribe to python-chinese-request在lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request在lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese




-- 
Blog°á¼ÒÁË

ÁõöÎ
March.Liu
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070206/68fc06da/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号