【问题标题】:multiline checkbox in wxpythonwxpython中的多行复选框
【发布时间】:2009-09-23 13:52:06
【问题描述】:

我正在使用 wxpython (2.8) 和 python 2.5。 是否可以强制 wx.CheckBox 在多个上显示其标签 线? 我希望能够与 wx.StaticText.Wrap(width) 做同样的事情

见附件示例:wx.CheckBox 是 200 像素宽,但它是 标签不适合此空间。

非常感谢任何帮助! 非常感谢 毛罗

#example starts here

import wx


class MyFrame(wx.Frame):
   def __init__(self):
       wx.Frame.__init__(self,  None, title="Hello World", size=
(300,200))

       self.panel = wx.Panel(self,  -1)
       myVSizer = wx.BoxSizer(wx.VERTICAL)

       #instantiating a checkbox 200 px wide. but the label is too
long
       cb = wx.CheckBox(self.panel,  -1,  label="This is a very very
long label for 200 pixel wide cb!",  size =wx.Size(200, -1))

       myVSizer.Add( cb, 1)

       self.panel.SetSizer(myVSizer)
       myVSizer.Layout()


app = wx.App(redirect=True)
top = MyFrame()
top.Show()
app.MainLoop()

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    这样的事情呢?柔性! (我已将其设置为单选按钮,以表明它的行为仍然像一个)

    import wx
    import textwrap
    
    class MultilineRadioButton(wx.RadioButton):
            def __init__(self, parent, id=-1, label=wx.EmptyString, wrap=10, pos=wx.DefaultPosition, size=wx.DefaultSize, style=0, validator=wx.DefaultValidator, name=wx.RadioButtonNameStr):
                wx.RadioButton.__init__(self,parent,id,'',pos,size,style,validator,name)
                self._label = label
                self._wrap = wrap
                lines = self._label.split('\n')
                self._wrappedLabel = []
                for line in lines:
                    self._wrappedLabel.extend(textwrap.wrap(line,self._wrap))
    
                self._textHOffset = 20
                dc = wx.ClientDC(self)
                font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
                dc.SetFont(font)
                maxWidth = 0
                totalHeight = 0
                lineHeight = 0
                for line in self._wrappedLabel:
                    width, height = dc.GetTextExtent(line)
                    maxWidth = max(maxWidth,width)
                    lineHeight = height
                    totalHeight += lineHeight 
    
                self._textHeight = totalHeight
    
                self.SetInitialSize(wx.Size(self._textHOffset + maxWidth,totalHeight))
                self.Bind(wx.EVT_PAINT, self.OnPaint)
    
            def OnPaint(self, event):
                dc = wx.PaintDC(self)
                self.Draw(dc)
                self.RefreshRect(wx.Rect(0,0,self._textHOffset,self.GetSize().height))
                event.Skip()
    
            def Draw(self, dc):
                dc.Clear()
                font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
                dc.SetFont(font)
                height = self.GetSize().height
                if height > self._textHeight:
                    offset = height / 2 - self._textHeight / 2
                else:
                    offset = 0
                for line in self._wrappedLabel:
                    width, height = dc.GetTextExtent(line)
                    dc.DrawText(line,self._textHOffset,offset)
                    offset += height
    
    
    class HFrame(wx.Frame):
       def __init__(self,pos=wx.DefaultPosition):
           wx.Frame.__init__(self,None,title="Hello World",size=wx.Size(600,400),pos=pos)
    
           self.panel = wx.Panel(self,-1)
           sizer = wx.BoxSizer(wx.HORIZONTAL)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           self.panel.SetSizer(sizer)
           sizer.Layout()
    
    
    class VFrame(wx.Frame):
       def __init__(self,pos=wx.DefaultPosition):
           wx.Frame.__init__(self,None,title="Hello World",size=wx.Size(600,400),pos=pos)
    
           self.panel = wx.Panel(self,-1)
           sizer = wx.BoxSizer(wx.VERTICAL)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           cb = RadioButton(self.panel,-1,label="This is a very very long label for the control!",wrap=10)
           sizer.Add(cb,1)
    
           self.panel.SetSizer(sizer)
           sizer.Layout()
    
    
    app = wx.App(redirect=False)
    htop = HFrame(pos=wx.Point(0,50))
    htop.Show()
    vtop = VFrame(pos=wx.Point(650,50))
    vtop.Show()
    app.MainLoop()
    

    【讨论】:

    • 伟大的卢卡,这是完美的解决方案!并感谢分享。这是我希望看到合并到 wxPython 本身的功能。 (对于那些无法运行示例的人:有一点错字,只需将 VFrame 和 HFrame 构造函数中的所有 'RadioButton' 实例替换为 'MultilineRadioButton' 即可。)
    【解决方案2】:

    不要使用带有文本的复选框,而是使用带有静态文本的无标签复选框以获得所需的效果,例如

    import wx
    
    
    class MyFrame(wx.Frame):
       def __init__(self):
          wx.Frame.__init__(self,  None, title="Hello World", size=(300,200))
    
          self.panel = wx.Panel(self,  -1)
          myVSizer = wx.BoxSizer(wx.VERTICAL)
    
          # use checkbox + static text to wrap the text
          myHSizer = wx.BoxSizer(wx.HORIZONTAL)
          cb = wx.CheckBox(self.panel,  -1,  label="")
          label = wx.StaticText(self.panel, label="This is a very very long label for 100 pixel wide cb!", size=(100,-1))
          label.Wrap(100)
          myHSizer.Add(cb, border=5, flag=wx.ALL)
          myHSizer.Add(label, border=5, flag=wx.ALL)
    
          myVSizer.Add(myHSizer)
    
          self.panel.SetSizer(myVSizer)
          myVSizer.Layout()
    
    
    app = wx.App(redirect=True)
    top = MyFrame()
    top.Show()
    app.MainLoop()
    

    这带来了额外的好处,您可以使用不同的布局将文本中心设置为复选框,或者在左侧或右侧或任何其他位置

    【讨论】:

      【解决方案3】:

      将标签更改为

      label="This is a very very\n long label for 200\n pixel wide cb!"
      

      应该这样做。

      也就是说,输入明确的\n 字符。

      【讨论】:

      • 另外,我发布了一个屏幕截图,以确保我们在这里谈论的是同一件事,但我认为这就是你想要的,对吧?
      • 是的,这就是我想要的。我实际上在 Windows 上(不是一个选择,一个约束......)无论如何谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-17
      • 2014-05-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-13
      • 2023-03-21
      相关资源
      最近更新 更多