📄 comboctrl.py
字号:
self.Dismiss()
evt.Skip()
#----------------------------------------------------------------------
# Here we subclass wx.combo.ComboCtrl to do some custom popup animation
CUSTOM_COMBOBOX_ANIMATION_DURATION = 200
class ComboCtrlWithCustomPopupAnim(wx.combo.ComboCtrl):
def __init__(self, *args, **kw):
wx.combo.ComboCtrl.__init__(self, *args, **kw)
self.Bind(wx.EVT_TIMER, self.OnTimer)
self.aniTimer = wx.Timer(self)
def AnimateShow(self, rect, flags):
self.aniStart = wx.GetLocalTimeMillis()
self.aniRect = wx.Rect(*rect)
self.aniFlags = flags
dc = wx.ScreenDC()
bmp = wx.EmptyBitmap(rect.width, rect.height)
mdc = wx.MemoryDC(bmp)
if "wxMac" in wx.PlatformInfo:
pass
else:
mdc.Blit(0, 0, rect.width, rect.height, dc, rect.x, rect.y)
del mdc
self.aniBackBitmap = bmp
self.aniTimer.Start(10, wx.TIMER_CONTINUOUS)
self.OnTimer(None)
return False
def OnTimer(self, evt):
stopTimer = False
popup = self.GetPopupControl().GetControl()
rect = self.aniRect
dc = wx.ScreenDC()
if self.IsPopupWindowState(self.Hidden):
stopTimer = True
else:
pos = wx.GetLocalTimeMillis() - self.aniStart
if pos < CUSTOM_COMBOBOX_ANIMATION_DURATION:
# Actual animation happens here
width = rect.width
height = rect.height
center_x = rect.x + (width/2)
center_y = rect.y + (height/2)
dc.SetPen( wx.BLACK_PEN )
dc.SetBrush( wx.TRANSPARENT_BRUSH )
w = (((pos*256)/CUSTOM_COMBOBOX_ANIMATION_DURATION)*width)/256
ratio = float(w) / float(width)
h = int(height * ratio)
dc.DrawBitmap( self.aniBackBitmap, rect.x, rect.y )
dc.DrawRectangle( center_x - w/2, center_y - h/2, w, h )
else:
stopTimer = True
if stopTimer:
dc.DrawBitmap( self.aniBackBitmap, rect.x, rect.y )
popup.Move( (0, 0) )
self.aniTimer.Stop()
self.DoShowPopup( rect, self.aniFlags )
#----------------------------------------------------------------------
# FileSelectorCombo displays a dialog instead of a popup control, it
# also uses a custom bitmap on the combo button.
class FileSelectorCombo(wx.combo.ComboCtrl):
def __init__(self, *args, **kw):
wx.combo.ComboCtrl.__init__(self, *args, **kw)
# make a custom bitmap showing "..."
bw, bh = 14, 16
bmp = wx.EmptyBitmap(bw,bh)
dc = wx.MemoryDC(bmp)
# clear to a specific background colour
bgcolor = wx.Colour(255,254,255)
dc.SetBackground(wx.Brush(bgcolor))
dc.Clear()
# draw the label onto the bitmap
label = "..."
font = wx.SystemSettings.GetFont(wx.SYS_DEFAULT_GUI_FONT)
font.SetWeight(wx.FONTWEIGHT_BOLD)
dc.SetFont(font)
tw,th = dc.GetTextExtent(label)
dc.DrawText(label, (bw-tw)/2, (bw-tw)/2)
del dc
# now apply a mask using the bgcolor
bmp.SetMaskColour(bgcolor)
# and tell the ComboCtrl to use it
self.SetButtonBitmaps(bmp, True)
# Overridden from ComboCtrl, called when the combo button is clicked
def OnButtonClick(self):
path = ""
name = ""
if self.GetValue():
path, name = os.path.split(self.GetValue())
dlg = wx.FileDialog(self, "Choose File", path, name,
"All files (*.*)|*.*", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.SetValue(dlg.GetPath())
dlg.Destroy()
self.SetFocus()
# Overridden from ComboCtrl to avoid assert since there is no ComboPopup
def DoSetPopupControl(self, popup):
pass
#----------------------------------------------------------------------
class TestPanel(wx.Panel):
def __init__(self, parent, log):
self.log = log
wx.Panel.__init__(self, parent, -1)
fgs = wx.FlexGridSizer(cols=3, hgap=10, vgap=10)
cc = self.MakeLCCombo(log=self.log)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, "wx.ComboCtrl with a ListCtrl popup"))
cc = self.MakeLCCombo(style=wx.CB_READONLY)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, " Read-only"))
cc = self.MakeLCCombo()
cc.SetButtonPosition(side=wx.LEFT)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, " Button on the left"))
cc = self.MakeLCCombo()
cc.SetPopupMaxHeight(250)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, " Max height of popup set"))
cc = wx.combo.ComboCtrl(self, size=(250,-1))
tcp = TreeCtrlComboPopup()
cc.SetPopupControl(tcp)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, "TreeCtrl popup"))
# add some items to the tree
for i in range(5):
item = tcp.AddItem('Item %d' % (i+1))
for j in range(15):
tcp.AddItem('Subitem %d-%d' % (i+1, j+1), parent=item)
cc = ComboCtrlWithCustomPopupAnim(self, size=(250, -1))
popup = ListCtrlComboPopup()
cc.SetPopupMaxHeight(150)
cc.SetPopupControl(popup)
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, "Custom popup animation"))
for word in "How cool was that!? Way COOL!".split():
popup.AddItem(word)
if "wxMac" in wx.PlatformInfo:
cc.SetValue("Sorry, animation not working yet on Mac")
cc = FileSelectorCombo(self, size=(250, -1))
fgs.Add(cc)
fgs.Add((10,10))
fgs.Add(wx.StaticText(self, -1, "Custom popup action, and custom button bitmap"))
box = wx.BoxSizer()
box.Add(fgs, 1, wx.EXPAND|wx.ALL, 20)
self.SetSizer(box)
def MakeLCCombo(self, log=None, style=0):
# Create a ComboCtrl
cc = wx.combo.ComboCtrl(self, style=style, size=(250,-1))
# Create a Popup
popup = ListCtrlComboPopup(log)
# Associate them with each other. This also triggers the
# creation of the ListCtrl.
cc.SetPopupControl(popup)
# Add some items to the listctrl.
for x in range(75):
popup.AddItem("Item-%02d" % x)
return cc
#----------------------------------------------------------------------
def runTest(frame, nb, log):
win = TestPanel(nb, log)
return win
#----------------------------------------------------------------------
overview = """<html><body>
<h2><center>wx.combo.ComboCtrl</center></h2>
A combo control is a generic combobox that allows a totally custom
popup. In addition it has other customization features. For instance,
position and size of the dropdown button can be changed.
</body></html>
"""
if __name__ == '__main__':
import sys,os
import run
run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -