gpt4 book ai didi

python - wxpython中的多行复选框

转载 作者:行者123 更新时间:2023-11-28 22:07:52 54 4
gpt4 key购买 nike

我正在使用 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()

最佳答案

这样的事情呢?柔性!(我把它变成了一个单选按钮,以表明它仍然像一个一样)

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()

关于python - wxpython中的多行复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466140/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com