wxPython: Gekapselte Events verwenden (noch ein Beispiel)
Verfasst: Donnerstag 8. Mai 2008, 12:57
Hallo!
mfg
Gerold

Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
import wx
import wx.lib.newevent
wx.SetDefaultPyEncoding("iso-8859-15")
class ControlPanel(wx.Panel):
# Event-Klassen und Eventbinder-Funktionen erstellen
(TopButtonEvent, EVT_TOP_BUTTON) = wx.lib.newevent.NewEvent()
(LeftButtonEvent, EVT_LEFT_BUTTON) = wx.lib.newevent.NewEvent()
(RightButtonEvent, EVT_RIGHT_BUTTON) = wx.lib.newevent.NewEvent()
(BottomButtonEvent, EVT_BOTTOM_BUTTON) = wx.lib.newevent.NewEvent()
def __init__(self, parent):
wx.Panel.__init__(self, parent, style = wx.TAB_TRAVERSAL | wx.BORDER)
# Sizer
fgrid = wx.FlexGridSizer(rows = 3, cols = 3)
fgrid.SetFlexibleDirection(wx.BOTH)
fgrid.AddGrowableCol(0)
fgrid.AddGrowableCol(1)
fgrid.AddGrowableCol(2)
self.SetSizer(fgrid)
fgrid.AddStretchSpacer()
# Top-Button
btn_top = wx.Button(self, label = "Top", name = "btn_top")
fgrid.Add(btn_top, 0, wx.ALIGN_CENTER | wx.ALL, 5)
btn_top.Bind(wx.EVT_BUTTON, self.on_navigation_button)
fgrid.AddStretchSpacer()
# Left-Button
btn_left = wx.Button(self, label = "Left", name = "btn_left")
fgrid.Add(btn_left, 0, wx.ALIGN_CENTER | wx.ALL, 5)
btn_left.Bind(wx.EVT_BUTTON, self.on_navigation_button)
fgrid.AddStretchSpacer()
# Right-Button
btn_right = wx.Button(self, label = "Right", name = "btn_right")
fgrid.Add(btn_right, 0, wx.ALIGN_CENTER | wx.ALL, 5)
btn_right.Bind(wx.EVT_BUTTON, self.on_navigation_button)
fgrid.AddStretchSpacer()
# Bottom-Button
btn_bottom = wx.Button(self, label = "Bottom", name = "btn_bottom")
fgrid.Add(btn_bottom, 0, wx.ALIGN_CENTER | wx.ALL, 5)
btn_bottom.Bind(wx.EVT_BUTTON, self.on_navigation_button)
fgrid.AddStretchSpacer()
def on_navigation_button(self, event):
"""
Löst das zugehörige Event aus.
Dieses Event tritt auf, wenn einer der Naviationsbuttons
geklickt wurde.
"""
button_name = event.GetEventObject().GetName()
event = {
"btn_top": self.TopButtonEvent,
"btn_left": self.LeftButtonEvent,
"btn_right": self.RightButtonEvent,
"btn_bottom": self.BottomButtonEvent,
}[button_name]()
self.ProcessEvent(event)
class DisplayPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style = wx.TAB_TRAVERSAL | wx.BORDER)
hbox = wx.BoxSizer(wx.HORIZONTAL)
self.SetSizer(hbox)
lab_display = wx.StaticText(self, style = wx.ST_NO_AUTORESIZE | wx.CENTER)
self.lab_display = lab_display
hbox.Add(lab_display, 1, wx.EXPAND | wx.ALL, 5)
def SetLabel(self, label):
self.lab_display.SetLabel(label)
self.Refresh()
self.Update()
class MyFrame(wx.Frame):
def __init__(
self, parent = None, title = "Example", size = wx.Size(400, 250)
):
wx.Frame.__init__(self, parent, -1, title, size = size)
main_panel = wx.Panel(self)
vbox_main = wx.BoxSizer(wx.VERTICAL)
main_panel.SetSizer(vbox_main)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox_main.Add(vbox, 1, wx.EXPAND | wx.ALL, 5)
# DisplayPanel
display_panel = DisplayPanel(main_panel)
vbox.Add(display_panel, 1, wx.EXPAND | wx.ALL, 5)
# ControlPanel
control_panel = ControlPanel(main_panel)
vbox.Add(control_panel, 0, wx.EXPAND | wx.ALL, 5)
# Events an das ControlPanel binden
control_panel.Bind(
control_panel.EVT_TOP_BUTTON,
lambda event: display_panel.SetLabel("Top")
)
control_panel.Bind(
control_panel.EVT_LEFT_BUTTON,
lambda event: display_panel.SetLabel("Left")
)
control_panel.Bind(
control_panel.EVT_RIGHT_BUTTON,
lambda event: display_panel.SetLabel("Right")
)
control_panel.Bind(
control_panel.EVT_BOTTOM_BUTTON,
lambda event: display_panel.SetLabel("Bottom")
)
def main():
"""Testing"""
app = wx.PySimpleApp()
f = MyFrame()
f.Center()
f.Show()
app.MainLoop()
if __name__ == "__main__":
main()
Gerold
