Python论坛  - 讨论区

标题:[python-chinese] wxWindows中,如何让一个控件充满整个对话框

2005年02月03日 星期四 17:22

hw_cg hw_cg at huawei.com
Thu Feb 3 17:22:57 HKT 2005

不好意思,对wxWindows不是很熟悉。
我写了以下代码,把窗体分成左右两部分SplitterWindow,左边放一个Tree,右边放一个ListCtrl

请问,如何让右边的ListCtrl充满窗体右边整个SplitterWindow部分?

#!/bin/python

import sys, os, time, traceback, types
import wx                  # This module uses the new wx namespace
import wx.html

import  wx.lib.mixins.listctrl  as  listmix

listctrldata = {
1 : ("Name1", "Type1", "Data1"),
2 : ("Name2", "Type2", "Data2"),
}

class DemoListCtrl(wx.ListCtrl,
       listmix.ListCtrlAutoWidthMixin,
       listmix.TextEditMixin):
       
 def __init__(self, parent, ID, pos=wx.DefaultPosition,
     size=wx.DefaultSize, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES):
  wx.ListCtrl.__init__(self, parent, ID, pos, size, style)

  listmix.ListCtrlAutoWidthMixin.__init__(self)
  self.Populate()
  listmix.TextEditMixin.__init__(self)       
  
 def Populate(self):
  # for normal, simple columns, you can add them like this:
  self.InsertColumn(0, "Name")
  self.InsertColumn(1, "Type")
  self.InsertColumn(2, "Data")

  items = listctrldata.items()
  for key, data in items:
   index = self.InsertStringItem(sys.maxint, data[0])
   self.SetStringItem(index, 0, data[0])
   self.SetStringItem(index, 1, data[1])
   self.SetStringItem(index, 2, data[2])
   self.SetItemData(index, key)

  self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
  self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
  self.SetColumnWidth(2, wx.LIST_AUTOSIZE)

  self.currentItem = 0
  
 def SetStringItem(self, index, col, data):
  if col in range(3):
   wx.ListCtrl.SetStringItem(self, index, col, data)
   wx.ListCtrl.SetStringItem(self, index, 3+col, str(len(data)))
  else:
   try:
    datalen = int(data)
   except:
    return
   wx.ListCtrl.SetStringItem(self, index, col, data)
   data = self.GetItem(index, col-3).GetText()
   wx.ListCtrl.SetStringItem(self, index, col-3, data[0:datalen])                            


class DemoWnd(wx.Frame):
 def __init__(self, parent, id, title):
  wx.Frame.__init__(self, parent, -1, title, size = (950, 720),
        style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE )
        
  self.SetMinSize((640,480))
  
  self.Centre(wx.BOTH)
  self.CreateStatusBar(1, wx.ST_SIZEGRIP)

  # Make a File menu
  self.mainmenu = wx.MenuBar()
  menu = wx.Menu()
 
  item = menu.Append(-1, 'E&xit;\tAlt-X', 'Get the heck outta here!')
  self.Bind(wx.EVT_MENU, self.OnFileExit, item)
  wx.App.SetMacExitMenuItemId(item.GetId())
  self.mainmenu.Append(menu, '&File;')
  
  self.SetMenuBar(self.mainmenu)
  
  splitter = wx.SplitterWindow(self, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE )
  self.splitter = splitter
  splitter2 = wx.SplitterWindow(splitter, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE | wx.SP_3D)
  self.splitter2 = splitter2
  
  def EmptyHandler(evt): pass

  # Create a TreeCtrl
  tID = wx.NewId()
  self.treeMap = {}
  self.tree = wx.TreeCtrl(splitter, tID, style =
        wx.TR_DEFAULT_STYLE #| wx.TR_HAS_VARIABLE_ROW_HEIGHT
          )
  root = self.tree.AddRoot("wxPython Overview")
  
  # Create a ListCtrl
  tID = wx.NewId()
  self.list = DemoListCtrl(splitter2, tID,
         style=wx.LC_REPORT
         | wx.BORDER_NONE
         | wx.LC_SORT_ASCENDING
         )  
  #self.Bind(wx.EVT_SIZE, self.OnSize)
  
  # add the windows to the splitter and split it.
  splitter.SplitVertically(self.tree, splitter2, 200)  
  #splitter2.SplitHorizontally(self.list, -160)

  splitter.SetMinimumPaneSize(120)
  splitter2.SetMinimumPaneSize(60)

  # Make the splitter on the right expand the top window when resized
  def SplitterOnSize(evt):
   splitter = evt.GetEventObject()
   sz = splitter.GetSize()
   splitter.SetSashPosition(sz.height - 160, False)
   evt.Skip()

  splitter2.Bind(wx.EVT_SIZE, SplitterOnSize)  
 #---------------------------------------------
 # Menu methods
 def OnFileExit(self, *event):
  self.Close()
 
class App(wx.App):
 def OnInit(self):
  frame = DemoWnd(None, -1, "TestDemo")
  frame.Show()
  return True

app = App(0)
app.MainLoop()


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

2005年02月03日 星期四 17:40

limodou limodou at gmail.com
Thu Feb 3 17:40:18 HKT 2005

在两个窗口上放两个panel,然后将tree和listctrl分别放在两个panel上。同时可
以考虑使用boxsizer来控制tree和listctrl的大小。

hw_cg wrote:
> 不好意思,对wxWindows不是很熟悉。
> 我写了以下代码,把窗体分成左右两部分SplitterWindow,左边放一个Tree,右边放一个ListCtrl
> 
> 请问,如何让右边的ListCtrl充满窗体右边整个SplitterWindow部分?
> 
> #!/bin/python
> 
> import sys, os, time, traceback, types
> import wx                  # This module uses the new wx namespace
> import wx.html
> 
> import  wx.lib.mixins.listctrl  as  listmix
> 
> listctrldata = {
> 1 : ("Name1", "Type1", "Data1"),
> 2 : ("Name2", "Type2", "Data2"),
> }
> 
> class DemoListCtrl(wx.ListCtrl,
>        listmix.ListCtrlAutoWidthMixin,
>        listmix.TextEditMixin):
>        
>  def __init__(self, parent, ID, pos=wx.DefaultPosition,
>      size=wx.DefaultSize, style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES):
>   wx.ListCtrl.__init__(self, parent, ID, pos, size, style)
> 
>   listmix.ListCtrlAutoWidthMixin.__init__(self)
>   self.Populate()
>   listmix.TextEditMixin.__init__(self)       
>   
>  def Populate(self):
>   # for normal, simple columns, you can add them like this:
>   self.InsertColumn(0, "Name")
>   self.InsertColumn(1, "Type")
>   self.InsertColumn(2, "Data")
> 
>   items = listctrldata.items()
>   for key, data in items:
>    index = self.InsertStringItem(sys.maxint, data[0])
>    self.SetStringItem(index, 0, data[0])
>    self.SetStringItem(index, 1, data[1])
>    self.SetStringItem(index, 2, data[2])
>    self.SetItemData(index, key)
> 
>   self.SetColumnWidth(0, wx.LIST_AUTOSIZE)
>   self.SetColumnWidth(1, wx.LIST_AUTOSIZE)
>   self.SetColumnWidth(2, wx.LIST_AUTOSIZE)
> 
>   self.currentItem = 0
>   
>  def SetStringItem(self, index, col, data):
>   if col in range(3):
>    wx.ListCtrl.SetStringItem(self, index, col, data)
>    wx.ListCtrl.SetStringItem(self, index, 3+col, str(len(data)))
>   else:
>    try:
>     datalen = int(data)
>    except:
>     return
>    wx.ListCtrl.SetStringItem(self, index, col, data)
>    data = self.GetItem(index, col-3).GetText()
>    wx.ListCtrl.SetStringItem(self, index, col-3, data[0:datalen])                            
> 
> 
> class DemoWnd(wx.Frame):
>  def __init__(self, parent, id, title):
>   wx.Frame.__init__(self, parent, -1, title, size = (950, 720),
>         style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE )
>         
>   self.SetMinSize((640,480))
>   
>   self.Centre(wx.BOTH)
>   self.CreateStatusBar(1, wx.ST_SIZEGRIP)
> 
>   # Make a File menu
>   self.mainmenu = wx.MenuBar()
>   menu = wx.Menu()
>  
>   item = menu.Append(-1, 'E&xit;\tAlt-X', 'Get the heck outta here!')
>   self.Bind(wx.EVT_MENU, self.OnFileExit, item)
>   wx.App.SetMacExitMenuItemId(item.GetId())
>   self.mainmenu.Append(menu, '&File;')
>   
>   self.SetMenuBar(self.mainmenu)
>   
>   splitter = wx.SplitterWindow(self, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE )
>   self.splitter = splitter
>   splitter2 = wx.SplitterWindow(splitter, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE | wx.SP_3D)
>   self.splitter2 = splitter2
>   
>   def EmptyHandler(evt): pass
> 
>   # Create a TreeCtrl
>   tID = wx.NewId()
>   self.treeMap = {}
>   self.tree = wx.TreeCtrl(splitter, tID, style =
>         wx.TR_DEFAULT_STYLE #| wx.TR_HAS_VARIABLE_ROW_HEIGHT
>           )
>   root = self.tree.AddRoot("wxPython Overview")
>   
>   # Create a ListCtrl
>   tID = wx.NewId()
>   self.list = DemoListCtrl(splitter2, tID,
>          style=wx.LC_REPORT
>          | wx.BORDER_NONE
>          | wx.LC_SORT_ASCENDING
>          )  
>   #self.Bind(wx.EVT_SIZE, self.OnSize)
>   
>   # add the windows to the splitter and split it.
>   splitter.SplitVertically(self.tree, splitter2, 200)  
>   #splitter2.SplitHorizontally(self.list, -160)
> 
>   splitter.SetMinimumPaneSize(120)
>   splitter2.SetMinimumPaneSize(60)
> 
>   # Make the splitter on the right expand the top window when resized
>   def SplitterOnSize(evt):
>    splitter = evt.GetEventObject()
>    sz = splitter.GetSize()
>    splitter.SetSashPosition(sz.height - 160, False)
>    evt.Skip()
> 
>   splitter2.Bind(wx.EVT_SIZE, SplitterOnSize)  
>  #---------------------------------------------
>  # Menu methods
>  def OnFileExit(self, *event):
>   self.Close()
>  
> class App(wx.App):
>  def OnInit(self):
>   frame = DemoWnd(None, -1, "TestDemo")
>   frame.Show()
>   return True
> 
> app = App(0)
> app.MainLoop()
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese

-- 
I love python!
My Blog: http://www.donews.net/limodou


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

2005年02月04日 星期五 09:31

Qiangning Hong hongqn at gmail.com
Fri Feb 4 09:31:39 HKT 2005

hw_cg wrote:
> 我写了以下代码,把窗体分成左右两部分SplitterWindow,左边放一个Tree,右边放一个ListCtrl
> 
> 请问,如何让右边的ListCtrl充满窗体右边整个SplitterWindow部分?
[snip]
>   
>   splitter = wx.SplitterWindow(self, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE )
>   self.splitter = splitter
>   splitter2 = wx.SplitterWindow(splitter, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE | wx.SP_3D)
>   self.splitter2 = splitter2

你在splitter的右边放的是一个SplitterWindow,不是ListCtrl。

[snip]
>   # Create a ListCtrl
>   tID = wx.NewId()
>   self.list = DemoListCtrl(splitter2, tID,
>          style=wx.LC_REPORT
>          | wx.BORDER_NONE
>          | wx.LC_SORT_ASCENDING
>          )  
>   #self.Bind(wx.EVT_SIZE, self.OnSize)

你把ListCtrl放在splitter2下面了。

[snip]

你的splitter2只设置了一个子窗口,当然显示不正确。

解决方法一:
  删除splitter2,直接将ListCtrl放在splitter下

解决方法二:
  给splitter2再加一个子窗口,然后用splitter2.SplitHorizontally分割。



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

2005年02月04日 星期五 09:48

hw_cg hw_cg at huawei.com
Fri Feb 4 09:48:54 HKT 2005

明白了,原来是自己对splitter的理解不正确,采用方法二多了一个窗口不符合我的要求,
采用方法一,解决问题了。


----- Original Message ----- 
From: "Qiangning Hong" <hongqn at gmail.com>
To: <python-chinese at lists.python.cn>
Sent: Friday, February 04, 2005 9:31 AM
Subject: Re: [python-chinese] wxWindows中,如何让一个控件充满整个对话框?


> hw_cg wrote:
> > 我写了以下代码,把窗体分成左右两部分SplitterWindow,左边放一个Tree,右边放一个ListCtrl
> > 
> > 请问,如何让右边的ListCtrl充满窗体右边整个SplitterWindow部分?
> [snip]
> >   
> >   splitter = wx.SplitterWindow(self, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE )
> >   self.splitter = splitter
> >   splitter2 = wx.SplitterWindow(splitter, -1, style=wx.CLIP_CHILDREN | wx.SP_LIVE_UPDATE | wx.SP_3D)
> >   self.splitter2 = splitter2
> 
> 你在splitter的右边放的是一个SplitterWindow,不是ListCtrl。
> 
> [snip]
> >   # Create a ListCtrl
> >   tID = wx.NewId()
> >   self.list = DemoListCtrl(splitter2, tID,
> >          style=wx.LC_REPORT
> >          | wx.BORDER_NONE
> >          | wx.LC_SORT_ASCENDING
> >          )  
> >   #self.Bind(wx.EVT_SIZE, self.OnSize)
> 
> 你把ListCtrl放在splitter2下面了。
> 
> [snip]
> 
> 你的splitter2只设置了一个子窗口,当然显示不正确。
> 
> 解决方法一:
>   删除splitter2,直接将ListCtrl放在splitter下
> 
> 解决方法二:
>   给splitter2再加一个子窗口,然后用splitter2.SplitHorizontally分割。
> 
> _______________________________________________
> python-chinese list
> python-chinese at lists.python.cn
> http://python.cn/mailman/listinfo/python-chinese

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

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

    你的回复:

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

    Zeuux © 2025

    京ICP备05028076号