Skip to main content
 首页 » 编程设计

python之响应式静态框

2025年02月15日21落叶无声

我想创建某种日志框来流式传输 python 应用程序的控制台输出。

为了在 UI 中获得更好的外观,我想将 TextCtrl 放入 StaticBox 中,理想情况下会在用户调整主框架大小时展开它。我已经尝试添加一个 GridBagSizer,它允许使用 AddGrowableCol(index) 和 AddGrowableRow(index) 方法。然而,使该行“可增长”没有任何效果,目前它只能水平工作:

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
 
import wx 
 
class Example(wx.Frame): 
 
    def __init__(self, parent, title):     
        super(Example, self).__init__(parent, title=title,  
            size=(450, 350)) 
 
        self.InitUI() 
        self.Centre() 
        self.Show()      
 
    def InitUI(self): 
 
        self.main_sizer = wx.GridBagSizer(5, 5) 
 
        # Create the grouping box and sizer for the outline 
        self.box = wx.StaticBox(self, -1, "Logging") 
        self.bsizer = wx.StaticBoxSizer(self.box, wx.VERTICAL) 
 
        # Create the sizer and place controls within box 
        self.gbs = wx.GridBagSizer(5,5) 
 
        self.log_ctrl = wx.TextCtrl(self, size=(-1, 200), style=wx.TE_MULTILINE|wx.VSCROLL) 
        self.gbs.Add(self.log_ctrl, (0,0), flag=wx.EXPAND|wx.ALL, border=10) 
 
        # Place the control inside group box 
        self.bsizer.Add(self.gbs, flag=wx.EXPAND) 
        self.gbs.AddGrowableCol(0) 
 
        # Adding to the main sizer 
        self.main_sizer.Add(self.bsizer, pos=(0, 0), span=(1, 30), flag=wx.EXPAND|wx.BOTTOM) 
        self.main_sizer.AddGrowableCol(0) 
 
        # Place the static group box sizer within the border frame 
        # Creating a border that the static box will sit inside 
        self.border = wx.BoxSizer() 
        self.border.Add(self.main_sizer, 1000, wx.ALL, 15) 
        self.SetSizerAndFit(self.border) 
 
 
if __name__ == '__main__': 
 
    app = wx.App() 
    Example(None, title="Example") 
    app.MainLoop() 

问题: 是否有巧妙的方法将 wx.TextCtrl 元素添加到 wx.StaticBox 以填充主窗口垂直以及水平展开时?

请您参考如下方法:

一些事情:

--- original.py 2016-11-24 08:20:56.958686427 +0100 
+++ modified.py 2016-11-24 08:21:53.879980674 +0100 
@@ -24,21 +24,23 @@ 
         # Create the sizer and place controls within box 
         self.gbs = wx.GridBagSizer(5,5) 
 
-        self.log_ctrl = wx.TextCtrl(self, size=(-1, 200), style=wx.TE_MULTILINE|wx.VSCROLL) 
+        self.log_ctrl = wx.TextCtrl(self.box, size=(-1, 200), style=wx.TE_MULTILINE|wx.VSCROLL) 
         self.gbs.Add(self.log_ctrl, (0,0), flag=wx.EXPAND|wx.ALL, border=10) 
 
         # Place the control inside group box 
-        self.bsizer.Add(self.gbs, flag=wx.EXPAND) 
+        self.bsizer.Add(self.gbs, proportion=1, flag=wx.EXPAND) 
         self.gbs.AddGrowableCol(0) 
+        self.gbs.AddGrowableRow(0) 
 
         # Adding to the main sizer 
         self.main_sizer.Add(self.bsizer, pos=(0, 0), span=(1, 30), flag=wx.EXPAND|wx.BOTTOM) 
         self.main_sizer.AddGrowableCol(0) 
+        self.main_sizer.AddGrowableRow(0) 
 
         # Place the static group box sizer within the border frame 
         # Creating a border that the static box will sit inside 
         self.border = wx.BoxSizer() 
-        self.border.Add(self.main_sizer, 1000, wx.ALL, 15) 
+        self.border.Add(self.main_sizer, 1000, wx.ALL | wx.EXPAND, 15) 
         self.SetSizerAndFit(self.border) 
  • 首先,您显然忘记了执行 AddGrowableRow。
  • 其次,self.gbs 必须有 proportion=1(或更多),否则它不会增长。
  • 最后,您的主 sizer 不会在边界内扩展。

此外,我已将 self.box 设为 self.log_ctrl 的父级,这应该是自 wx 2.9.1 以来的正确方法。

在您的示例中,GridBagSizers 是完全不必要的:

    def InitUI(self): 
        # Create the grouping box and sizer for the outline 
        self.box = wx.StaticBox(self, -1, "Logging") 
        self.bsizer = wx.StaticBoxSizer(self.box, wx.VERTICAL) 
 
        self.log_ctrl = wx.TextCtrl(self.box, size=(-1, 200), style=wx.TE_MULTILINE|wx.VSCROLL) 
        self.bsizer.Add(self.log_ctrl, proportion=1, flag=wx.EXPAND|wx.ALL, border=10) 
 
        # Place the static group box sizer within the border frame 
        # Creating a border that the static box will sit inside 
        self.border = wx.BoxSizer() 
        self.border.Add(self.bsizer, proportion=1, flag=wx.ALL | wx.EXPAND, border=15) 
        self.SetSizerAndFit(self.border)