Lund University / Lunarc / Python Lecture 1
wxPython
Grafiska gränssni< för Python
Lund University / Lunarc / Python Lecture 1
wxPython • Pla@ormsoberoende bibliotek för användargränssni< – Mac/Windows/Linux
• Program skrivna med wxPython anpassar utseendet eFer pla@ormen
Lund University / Lunarc / Python Lecture 1
Basklasser • wx.Window – Basklass för fönster
• wx.Control – Basklass för kontroller
• wx.ControlWithItem – Basklass för kontroller som innehåller data
Lund University / Lunarc / Python Lecture 1
Toppklasser • wx.PopupWindow • wx.ScrolledWindow • wx.Frame • wx.MDIParentFrame • wx.MDIChildFrame • wx.Dialog
Lund University / Lunarc / Python Lecture 1
Container-‐kontroller • Kontroller som innehåller andra kontroller – wx.ScrolledWindow – wx.Panel – wx.Spli<erWindow – wx.Notebook
Lund University / Lunarc / Python Lecture 1
Dynamiska kontroller • Kontroller som användare kan redigera – wx.ToggleBu<on – wx.Bu<on – wx.CheckBox – wx.TextCtrl – wx.ListBox – wx.ComboBox …
Lund University / Lunarc / Python Lecture 1
StaTska kontroller • Kontroller som visa staTska data – wx.StaTcBitmap – wx.StaTcBox – wx.Gauge – wx.StaTcText
Lund University / Lunarc / Python Lecture 1
Andra kontroller • wx.Toolbar – Implementerar e< verktygsfält i e< fönster
• wx.MenuBar – Implementerar en menyrad i e< fönster
• wx.StatusBar – Implementerar statusfält i e< fönster
Lund University / Lunarc / Python Lecture 1
Stommen i e< wxPython program
import wx app = wx.App() frame = wx.Frame(None, -‐1, 'My first') frame.Show() app.MainLoop()
Importera wxPython
InstanTera en applikaTonsklass
Skapa e< fönster
Visa fönster
Starta applikaTonens händelseloop
Lund University / Lunarc / Python Lecture 1
wx.Frame wx.Frame(wx.Window parent, int id=-‐1, string Ttle='', wx.Point pos = wx.DefaultPosiTon, wx.Size size = wx.DefaultSize, style = wx.DEFAULT_FRAME_STYLE, string name = "frame")
wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN
# No minimize box frame = wx.Frame(None, style=wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX) frame.Show(True)
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_size.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(250,200)) self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Setting the size") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_size2.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.SetSize((400,300)) self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Setting the size") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_move.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.SetSize((400,300)) self.Move((500,250)) self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Moving") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_center.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.SetSize((500,500)) self.Centre() self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Moving") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_maximize.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.Maximize() self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Moving") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Frame (frame_maximize.py)
import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) self.Maximize() self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Moving") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Händelsestyrd programmering • Programmets flöde styrs av händelser – Musklickningar, tangentnedtryckning och meddelanden
• Programmet oFa uppbyggt kring en huvudloop – Händelsedetektering – Händelsehantering
Lund University / Lunarc / Python Lecture 1
Händelser i wxPython • Det flesta kontroller i wxPython kan kopplas Tll en eller flera händelser
• En händelse kopplas Tll en Python-‐funkToner med hjälp av Bind(…) funkTonen
Lund University / Lunarc / Python Lecture 1
Händelser (event3.py) import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) button = wx.Button(panel, wx.ID_ANY, "Test", (10,10), (70,24)) self.Bind(wx.EVT_BUTTON, self.onButton, id=button.GetId()) self.Centre() self.Show(True) def onButton(self, event): print "Button clicked." app = wx.App() frame = EventWindow(None, -‐1, "Move event") app.MainLoop()
Kontroll som skall kopplas Tll händelse
Typ av händelse HändelsefunkTon Kontroll-‐id
Variabel med informaTon om händelsen.
Lund University / Lunarc / Python Lecture 1
Händelser (event1.py) import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) xLabel = wx.StaticText(self, -‐1, 'x:', (10,10)) yLabel = wx.StaticText(self, -‐1, 'y:', (10,30)) self.xValue = wx.StaticText(self, -‐1, '', (30, 10)) self.yValue = wx.StaticText(self, -‐1, '', (30, 30)) self.Bind(wx.EVT_MOVE, self.onMove) self.Centre() self.Show(True) def onMove(self, event): x, y = event.GetPosition() self.xValue.SetLabel(str(x)) self.yValue.SetLabel(str(y)) app = wx.App() frame = EventWindow(None, -‐1, "Move event") app.MainLoop()
StaTska textkontroller
Hantera fönsterförfly<ning
Lund University / Lunarc / Python Lecture 1
Händelser (event2.py) import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) xLabel = wx.StaticText(self, -‐1, 'x:', (10,10)) yLabel = wx.StaticText(self, -‐1, 'y:', (10,30)) self.xValue = wx.StaticText(self, -‐1, '', (30, 10)) self.yValue = wx.StaticText(self, -‐1, '', (30, 30)) self.Bind(wx.EVT_MOTION, self.onMotion) self.Bind(wx.EVT_MOUSE_EVENTS, self.onMouse) self.Centre() self.Show(True) def onMotion(self, event): x, y = event.GetPosition() self.xValue.SetLabel(str(x)) self.yValue.SetLabel(str(y)) def onMouse(self, event): if event.IsButton(): print event.GetButton() app = wx.App() frame = EventWindow(None, -‐1, "Move event") app.MainLoop()
StaTska textkontroller
Hantera musrörelser Hantera mushändelser
Lund University / Lunarc / Python Lecture 1
Menyer och verktygsfält
MenuBar
Menu
MenuItem MenuItem
Menu
MenuItem
Lund University / Lunarc / Python Lecture 1
Menyer (menubar1.py) import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) menuBar = wx.MenuBar() fileMenu = wx.Menu() fileMenu.Append(-‐1, "Quit", "Quit application") menuBar.Append(fileMenu, "&File") self.SetMenuBar(menuBar) self.SetSize((500,500)) self.Centre() self.Show(True) app = wx.App() myFrame = MyFrame(None, -‐1, "Menu example") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Menyer (menubar2.py) import wx class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) menuBar = wx.MenuBar() fileMenu = wx.Menu() fileMenu.Append(1, "&Quit\tCtrl-‐Q", "Quit application") menuBar.Append(fileMenu, "&File") self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.onQuit, id=1) self.SetSize((500,500)) self.Centre() self.Show(True) def onQuit(self, event): self.Close() app = wx.App() myFrame = MyFrame(None, -‐1, "Menu example 2") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Menyer (menubar3.py) import wx ID_QUIT = 99 class MyFrame(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) menuBar = wx.MenuBar() fileMenu = wx.Menu() fileMenu.Append(1, '&New') fileMenu.Append(2, '&Open') fileMenu.Append(3, '&Save') fileMenu.AppendSeparator() fileMenu.Append(ID_QUIT, "&Quit\tCtrl-‐Q", "Quit application") menuBar.Append(fileMenu, "&File") self.SetMenuBar(menuBar) self.Bind(wx.EVT_MENU, self.onNew, id=1) self.Bind(wx.EVT_MENU, self.onOpen, id=2) self.Bind(wx.EVT_MENU, self.onSave, id=3) self.Bind(wx.EVT_MENU, self.onQuit, id=ID_QUIT) self.SetSize((500,500)) self.Centre() self.Show(True) def onQuit(self, event): self.Close() def onNew(self, event): print "onNew:" def onOpen(self, event): print "onOpen:" def onSave(self, event): print "onSave:" app = wx.App() myFrame = MyFrame(None, -‐1, "Menu example 3") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Verktygsfält (toolbar1.py) def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) menuBar = wx.MenuBar() fileMenu = wx.Menu() fileMenu.Append(1, '&New') fileMenu.Append(2, '&Open') fileMenu.Append(3, '&Save') fileMenu.AppendSeparator() fileMenu.Append(ID_QUIT, "&Quit\tCtrl-‐Q", "Quit application") menuBar.Append(fileMenu, "&File") self.SetMenuBar(menuBar) toolbar = self.CreateToolBar() toolbar.AddLabelTool(ID_QUIT, "Quit", wx.Bitmap('./images/system-‐shutdown.png')) toolbar.SetToolBitmapSize((22,22)) toolbar.Realize() self.Bind(wx.EVT_MENU, self.onNew, id=1) self.Bind(wx.EVT_MENU, self.onOpen, id=2) self.Bind(wx.EVT_MENU, self.onSave, id=3) self.Bind(wx.EVT_MENU, self.onQuit, id=ID_QUIT) self.Bind(wx.EVT_TOOL, self.onQuit, id=ID_QUIT) self.SetSize((500,500)) self.Centre() self.Show(True)
Lund University / Lunarc / Python Lecture 1
Verktygsfält (toolbar2.py) def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title) ... self._toolbar = self.CreateToolBar() self._toolbar.AddLabelTool(1, "New", wx.Bitmap('./images/document-‐new.png')) self._toolbar.AddLabelTool(2, "Open", wx.Bitmap('./images/document-‐open.png')) self._toolbar.AddLabelTool(3, "Save", wx.Bitmap('./images/document-‐save.png')) self._toolbar.AddSeparator() self._toolbar.AddLabelTool(ID_QUIT, "Quit", wx.Bitmap('./images/system-‐shutdown.png')) self._toolbar.EnableTool(3, False) self._toolbar.SetToolBitmapSize((22,22)) self._toolbar.Realize() self.Bind(wx.EVT_MENU, self.onNew, id=1) self.Bind(wx.EVT_MENU, self.onOpen, id=2) self.Bind(wx.EVT_MENU, self.onSave, id=3) self.Bind(wx.EVT_MENU, self.onQuit, id=ID_QUIT) self.Bind(wx.EVT_TOOL, self.onNew, id=1) self.Bind(wx.EVT_TOOL, self.onOpen, id=2) self.Bind(wx.EVT_TOOL, self.onSave, id=3) self.Bind(wx.EVT_TOOL, self.onQuit, id=ID_QUIT)
Lund University / Lunarc / Python Lecture 1
Layouthantering • Två alternaTv – Absolut posiTonering – ”Sizers”
• Absolut posiTonering problemaTsk vid olika upplösningar, storlekar på fönster etc.
• ”Sizers” placerar dynamiskt ut kontroller i fönster med avseende på fönstrets storlek
Lund University / Lunarc / Python Lecture 1
wx.BoxSizer
wx.VERTICAL
wx.HORIZONTAL
Lund University / Lunarc / Python Lecture 1
wx.BoxSizer
Lund University / Lunarc / Python Lecture 1
wx.BoxSizer
box = wx.BoxSizer(integer orient) box.Add(wx.Window window, integer proporTon=0, integer flag = 0, integer border = 0)
wx.VERTICAL or wx.HORIZONTAL
proporTon = 0 (= Kontrollen kommer inte a< ändra storlek) proporTon > 0 (= Kontroller kommer a< anpassa sig) flag anger extra opToner för marginaler och skalning border anger marginalens storlek
Lund University / Lunarc / Python Lecture 1
layout1-‐layout4.py
Lund University / Lunarc / Python Lecture 1
Standarddialogrutor • Meddelandedialogrutor • Fildialogrutor • Färgvalsdialoger • …
Lund University / Lunarc / Python Lecture 1
Dialogrutor (dialogs1.py)
import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) button = wx.Button(panel, wx.ID_ANY, "Show", (10,10), (70,24)) self.Bind(wx.EVT_BUTTON, self.onButton, id=button.GetId()) self.Centre() self.Show(True) def onButton(self, event): wx.MessageBox('This is a simple message box', 'Info') app = wx.App() frame = EventWindow(None, -‐1, "Dialogs 1") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Dialogrutor (dialogs2.py)
import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) button = wx.Button(panel, wx.ID_ANY, "Show", (10,10), (70,24)) self.Bind(wx.EVT_BUTTON, self.onButton, id=button.GetId()) self.Centre() self.Show(True) def onButton(self, event): wx.MessageBox('This is a simple message box', 'Info') app = wx.App() frame = EventWindow(None, -‐1, "Dialogs 1") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Dialogrutor (dialogs3.py) import wx class EventWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) button = wx.Button(panel, wx.ID_ANY, "Show", (10,10), (70,24)) self.Bind(wx.EVT_BUTTON, self.onButton, id=button.GetId()) self.Centre() self.Show(True) def onButton(self, event): filename = wx.FileSelector("Open file", default_extension="*.*", wildcard="*.*", flags=wx.FD_OPEN) if filename != "": wx.MessageBox(filename, 'Info') app = wx.App() frame = EventWindow(None, -‐1, "Dialogs 1") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
Kontroller
Lund University / Lunarc / Python Lecture 1
wx.ToggleBu<on • Knappar med Tllstånd – Jämför av-‐ och på-‐knappar – Enabled/Disabled – SetValue(True/False) för a< sä<a Tllstånde – GetValue returnerar aktuellt Tllstånd
Lund University / Lunarc / Python Lecture 1
wx.ToggleBu<on import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) self.firstOption = wx.ToggleButton(panel, 1, "First", (10,10), (70,24)) self.secondOption = wx.ToggleButton(panel, 2, "Second", (10,38), (70,24)) self.thirdOption = wx.ToggleButton(panel, 3, "third", (10,64), (70,24)) self.Bind(wx.EVT_TOGGLEBUTTON, self.onToggle, id=self.firstOption.GetId()) self.Bind(wx.EVT_TOGGLEBUTTON, self.onToggle, id=self.secondOption.GetId()) self.Bind(wx.EVT_TOGGLEBUTTON, self.onToggle, id=self.thirdOption.GetId()) self.Centre() self.Show(True) def onToggle(self, event): if event.GetId() == 1: print "id = 1, value = ", self.firstOption.GetValue() elif event.GetId() == 2: print "id = 2, value = ", self.secondOption.GetValue() elif event.GetId() == 3: print "id = 2, value = ", self.thirdOption.GetValue() app = wx.App() frame = WidgetWindow(None, -‐1, "Widgets 1") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.ComboBox • Flervalskontroll/Textruta – Val av från e< begränsat urval – Textruta med lista med fördefinierade värden
• Kompakt kontroll om det finns lite plats • Val i listan kan kopplas Tll händelsen EVT_COMBOX – event.GetSelecTon() returnerar index för aktuellt val
– GetValue() / SetValue() kan användas för a< sä<a fördefinierat värde.
Lund University / Lunarc / Python Lecture 1
wx.ComboBox import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) choiceList = ["Ericsson", "Nokia", "Apple"] self.comboBox1 = wx.ComboBox(panel, -‐1, pos=(50, 170), size=(150, -‐1),
choices=choiceList, style=wx.CB_READONLY) self.comboBox1.SetValue("Apple") self.comboBox2 = wx.ComboBox(panel, -‐1, pos=(50, 200), size=(150, -‐1),
choices=choiceList, style=wx.CB_DROPDOWN) self.comboBox2.SetValue("Apple") self.comboBox3 = wx.ComboBox(panel, -‐1, pos=(50, 230), size=(150, -‐1),
choices=choiceList, style=wx.CB_SORT) self.comboBox3.SetValue("Apple") self.Bind(wx.EVT_COMBOBOX, self.onSelect1, id=self.comboBox1.GetId()) self.Bind(wx.EVT_COMBOBOX, self.onSelect2, id=self.comboBox2.GetId()) self.Bind(wx.EVT_COMBOBOX, self.onSelect3, id=self.comboBox3.GetId()) self.Centre() self.Show(True) def onSelect1(self, event): item = event.GetSelection() value = self.comboBox1.GetValue() print "item = ", item print "value = ", value
Lund University / Lunarc / Python Lecture 1
wx.CheckBox • Implementerar en ”kryssruta” • Kan vara True eller False • GetValue() / SetValue() används returnera och Tlldela kontrollen värden
Lund University / Lunarc / Python Lecture 1
wx.CheckBox import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) self.checkBox = wx.CheckBox(panel, -‐1, "Extra allt", (10,10)) self.checkBox.SetValue(True) self.Bind(wx.EVT_CHECKBOX, self.onCheck, id=self.checkBox.GetId()) self.Centre() self.Show(True) def onCheck(self, event): value = self.checkBox.GetValue() print "value = ", value app = wx.App() frame = WidgetWindow(None, -‐1, "Widgets 3") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.RadioBu<on • Används för flervalsfrågor där endast e< alternaTv kan väljas
• SetValue() / GetValue() används på samma sä< som för wx.CheckBox
• style=wx.RB_GROUP vid instanTering anger a< en ny grupp skall skapas. EFerföljande radiorutor kommer a< Tllhöra samma grupp Tll dess a< style=wx.RB_GROUP anges igen.
Lund University / Lunarc / Python Lecture 1
wx.CheckBox import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) self.radioButton1 = wx.RadioButton(panel, -‐1, 'Value A', (10, 10), style=wx.RB_GROUP) self.radioButton2 = wx.RadioButton(panel, -‐1, 'Value B', (10, 30)) self.radioButton3 = wx.RadioButton(panel, -‐1, 'Value C', (10, 50)) self.Bind(wx.EVT_RADIOBUTTON, self.onSelect, id=self.radioButton1.GetId()) self.Bind(wx.EVT_RADIOBUTTON, self.onSelect, id=self.radioButton2.GetId()) self.Bind(wx.EVT_RADIOBUTTON, self.onSelect, id=self.radioButton3.GetId()) self.statusBar = self.CreateStatusBar(3) self.radioButton1.SetValue(True) self.Centre() self.Show(True) def onSelect(self, event): state1 = str(self.radioButton1.GetValue()) state2 = str(self.radioButton2.GetValue()) state3 = str(self.radioButton3.GetValue()) self.statusBar.SetStatusText(state1, 0) self.statusBar.SetStatusText(state2, 1) self.statusBar.SetStatusText(state3, 2) app = wx.App() frame = WidgetWindow(None, -‐1, "Widgets 4") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Gauge • Visar ”termometer”/”mätare” för hur långt en process har kommit
• StaTsk kontroll, dvs användaren kan inte direkt påverka kontrollen.
• SetValue()/GetValue()
Lund University / Lunarc / Python Lecture 1
wx.Gauge import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) self.gauge = wx.Gauge(panel, -‐1, 50, pos=(10,10), size=(250, 25)) addButton = wx.Button(panel, -‐1, "Add", pos=(10,40), size=(70,30)) decreaseButton = wx.Button(panel, -‐1, "Decrease", pos=(10,80), size=(70,30)) self.Bind(wx.EVT_BUTTON, self.onAdd, id=addButton.GetId()) self.Bind(wx.EVT_BUTTON, self.onDecrease, id=decreaseButton.GetId()) self.Centre() self.Show(True) def onAdd(self, event): value = self.gauge.GetValue() value += 1 self.gauge.SetValue(value) def onDecrease(self, event): value = self.gauge.GetValue() value -‐= 1 self.gauge.SetValue(value) app = wx.App() frame = WidgetWindow(None, -‐1, "Widgets 4") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.Slider • Skjutreglage för a< snabbt kunna ge värden utan a< behöva ange dem numeriskt
• OpToner finns för a< visa skala och fasta stopp.
Lund University / Lunarc / Python Lecture 1
wx.Slider import wx class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) self.slider = wx.Slider(panel, -‐1, 200, 150, 500, (10, 10), (250, -‐1), wx.SL_AUTOTICKS | wx.SL_HORIZONTAL | wx.SL_LABELS) self.Bind(wx.EVT_SLIDER, self.onChange, id=self.slider.GetId()) self.Centre() self.Show(True) def onChange(self, event): print "value = ", self.slider.GetValue() app = wx.App() frame = WidgetWindow(None, -‐1, "Widgets 5") app.MainLoop()
Lund University / Lunarc / Python Lecture 1
wx.ListBox • Visar en ruta med e< antal valbara alternaTv • Används för a< välja alternaTv från en stor mängd data – Rullningslister visas om rutan inte räcker Tll
• Stöjder mulTpla val
Lund University / Lunarc / Python Lecture 1
wx.ListBox class WidgetWindow(wx.Frame): def __init__(self, parent, id, title): wx.Frame.__init__(self, parent, id, title, size=(390,350)) panel = wx.Panel(self, -‐1) listItems = ["asdsad", "asdasdaasd", "asdasda", "asdsada"] self.listBox = wx.ListBox(panel, 26, (10, 10), (170, 130), listItems, wx.LB_SINGLE) addButton = wx.Button(panel, -‐1, "Add", pos=(10,150), size=(70,30)) removeButton = wx.Button(panel, -‐1, "Remove", pos=(10,190), size=(70,30)) clearButton = wx.Button(panel, -‐1, "Clear", pos=(10,230), size=(70,30)) self.Bind(wx.EVT_BUTTON, self.onAdd, id=addButton.GetId()) self.Bind(wx.EVT_BUTTON, self.onRemove, id=removeButton.GetId()) self.Bind(wx.EVT_BUTTON, self.onClear, id=clearButton.GetId()) self.Bind(wx.EVT_LISTBOX, self.onChange, id=self.listBox.GetId()) self.Centre() self.Show(True) def onChange(self, event): print "value = ", self.listBox.GetSelection() def onAdd(self, event): self.listBox.Append("adasdsa") def onRemove(self, event): if self.listBox.GetSelection()>=0: self.listBox.Delete(self.listBox.GetSelection()) def onClear(self, event): self.listBox.Clear()
Lund University / Lunarc / Python Lecture 1
wxGlade
Lund University / Lunarc / Python Lecture 1
wxGlade • Grafiskt verkty för design av användargränssni< i wxPython
• Två-‐vägs verkyg – Genererar källkod för gränssni< – Kan uppdatera Tdigare genererad kod utan a< användarkod påverkas.
Lund University / Lunarc / Python Lecture 1
wxGlade utseende Verktygslåda med kontroller
Objek<räd med kontroller och fönster
Egenskapseditor