wxPython控件学习之wx.lib.scrolledpanel.ScrolledPanel
1. ScrolledPanel类,wx.ScrolledWindow的子类之一。提供了自动滚动条和滚动行为。此外也提供了wx.ScrolledWindow不具有的标签遍历管理功能(tab traversal management)
2. 方法集
SetupScrolling
(self, scroll_x, scroll_y, rate_x, rate_y, scrollToTop)该方法需要显示调用,绑定必须的的事件去完成适合的滚动
# -*- coding: UTF-8 -*- #------------------------------------------------------------------------------- # Name: 模块1 # Purpose: # # Author: ankier # # Created: 10/11/2012 # Copyright: (c) ankier 2012 # Licence: <your licence> #------------------------------------------------------------------------------- import wx import wx.lib.scrolledpanel as scrolled class ScrolledPanelFrame(wx.Frame): def __init__(self): wx.Frame.__init__(self, None, -1, 'Combo Box Example') mainFlexSizer = wx.FlexGridSizer(2,1,25,-1) panel = scrolled.ScrolledPanel(self, -1) headLabel = wx.StaticText(self, -1, "Head label, you will always see it") panelFlexSizer = wx.FlexGridSizer(3,4,6,6) staticText1 = wx.StaticText(panel, -1, "First name:") textBox1 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) staticText2 = wx.StaticText(panel, -1, "Second name:") textBox2 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) staticText3 = wx.StaticText(panel, -1, "Last name:") textBox3 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) staticText4 = wx.StaticText(panel, -1, "Sex:") textBox4 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) staticText5 = wx.StaticText(panel, -1, "First name:") textBox5 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) staticText6 = wx.StaticText(panel, -1, "Second name:") textBox6 = wx.wx.TextCtrl(panel, -1, size =(210, 50)) panelFlexSizer.AddMany([ (staticText1, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox1, 0, wx.SHAPED) ,(staticText2, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox2, 0, wx.SHAPED) ,(staticText3, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox3, 0, wx.SHAPED) ,(staticText4, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox4, 0, wx.SHAPED) ,(staticText5, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox5, 0, wx.SHAPED) ,(staticText6, 0, wx.SHAPED|wx.ALIGN_LEFT),(textBox6, 0, wx.SHAPED) ]) panelFlexSizer.AddGrowableCol(1) panelFlexSizer.AddGrowableCol(3) panel.SetSizerAndFit(panelFlexSizer) panel.SetAutoLayout(1) panel.SetupScrolling() mainFlexSizer.AddMany([ (headLabel, 0, wx.SHAPED|wx.ALIGN_LEFT), (panel, 1, wx.EXPAND) ]) mainFlexSizer.AddGrowableCol(0) mainFlexSizer.AddGrowableRow(1) self.SetSizerAndFit(mainFlexSizer) #由于Frame的MinSize的计算是有其sizer所包括的控件所占的空间,所决定的。 #所以如果我们想继续拖动Frame以改变其大小,我们需要重新计算和修正改Frame的MinSize, self.SetMinSize((300,200)) def main(): app = wx.PySimpleApp() ScrolledPanelFrame().Show() app.MainLoop() if __name__ == '__main__': main()
import wx import wx.lib.scrolledpanel as scrolled ######################################################################## class MyForm(wx.Frame): #---------------------------------------------------------------------- def __init__(self): wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(200,500)) # Add a panel so it looks the correct on all platforms self.panel = wx.Panel(self, wx.ID_ANY) # -------------------- # Scrolled panel stuff self.scrolled_panel = scrolled.ScrolledPanel(self.panel, -1, style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="panel1") self.scrolled_panel.SetAutoLayout(1) self.scrolled_panel.SetupScrolling() words = "A Quick Brown Insane Fox Jumped Over the Fence and Ziplined to Cover".split() self.spSizer = wx.BoxSizer(wx.VERTICAL) for word in words: text = wx.TextCtrl(self.scrolled_panel, value=word) self.spSizer.Add(text) self.scrolled_panel.SetSizer(self.spSizer) # -------------------- btn = wx.Button(self.panel, label="Add Widget") btn.Bind(wx.EVT_BUTTON, self.onAdd) panelSizer = wx.BoxSizer(wx.VERTICAL) panelSizer.Add(self.scrolled_panel, 1, wx.EXPAND) panelSizer.Add(btn) self.panel.SetSizer(panelSizer) #---------------------------------------------------------------------- def onAdd(self, event): new_text = wx.TextCtrl(self.scrolled_panel, value="New Text") self.spSizer.Add(new_text) self.scrolled_panel.Layout() self.scrolled_panel.SetupScrolling() # Run the program if __name__ == "__main__": app = wx.App(False) frame = MyForm().Show() app.MainLoop()