例子
在下面的示例中,滑块用于控制标签的大小。首先,将一个滑块对象放置在一个垂直的 box sizer 中,其下方是一个 StaticText。
self.sld = wx.Slider(pnl, value = 10, minValue = 1, maxValue = 100,
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
self.txt = wx.StaticText(pnl, label = 'Hello',style = wx.ALIGN_CENTER)
Wx.EVT_SLIDER binder 与 OnSliderScroll() 处理程序相关联。
self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll)
处理程序本身正在获取滑块的当前值并将其用作标签文本的字体大小。
def OnSliderScroll(self, e):
obj = e.GetEventObject()
val = obj.GetValue()
font = self.GetFont()
font.SetPointSize(self.sld.GetValue())
self.txt.SetFont(font)
完整的代码如下 -
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title,size = (250,150))
self.InitUI()
def InitUI(self):
pnl = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
self.sld = wx.Slider(pnl, value = 10, minValue = 1, maxValue = 100,
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
vbox.Add(self.sld,1,flag = wx.EXPAND | wx.ALIGN_CENTER_HORIZONTAL | wx.TOP, border = 20)
self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll)
self.txt = wx.StaticText(pnl, label = 'Hello',style = wx.ALIGN_CENTER)
vbox.Add(self.txt,1,wx.ALIGN_CENTRE_HORIZONTAL)
pnl.SetSizer(vbox)
self.Centre()
self.Show(True)
def OnSliderScroll(self, e):
obj = e.GetEventObject()
val = obj.GetValue()
font = self.GetFont()
font.SetPointSize(self.sld.GetValue())
self.txt.SetFont(font)
ex = wx.App()
Mywin(None,'Slider demo')
ex.MainLoop()
运行代码并尝试拖动滑块手柄以查看标签字体大小的变化。上面的代码产生以下输出 -