wxPython: Gekapselte Events verwenden (noch ein Beispiel)

Code-Stücke können hier veröffentlicht werden.
Antworten
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5554
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Telfs (Tirol)
Kontaktdaten:

Donnerstag 8. Mai 2008, 12:57

Hallo!

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()
mfg
Gerold
:-)
Zuletzt geändert von gerold am Donnerstag 8. Mai 2008, 13:10, insgesamt 1-mal geändert.
[url]http://halvar.at[/url] | [url=http://halvar.at/elektronik/kleiner_bascom_avr_kurs/]Kleiner Bascom AVR Kurs[/url]
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5554
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Telfs (Tirol)
Kontaktdaten:

Donnerstag 8. Mai 2008, 13:08

Hallo!

Und so sieht das gleiche Beispiel, ohne gekapselte Events aus:

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):
    
    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
        self.btn_top = wx.Button(self, label = "Top", name = "btn_top")
        fgrid.Add(self.btn_top, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        
        fgrid.AddStretchSpacer()
        
        # Left-Button
        self.btn_left = wx.Button(self, label = "Left", name = "btn_left")
        fgrid.Add(self.btn_left, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        
        fgrid.AddStretchSpacer()
        
        # Right-Button
        self.btn_right = wx.Button(self, label = "Right", name = "btn_right")
        fgrid.Add(self.btn_right, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        
        fgrid.AddStretchSpacer()
        
        # Bottom-Button
        self.btn_bottom = wx.Button(self, label = "Bottom", name = "btn_bottom")
        fgrid.Add(self.btn_bottom, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        
        fgrid.AddStretchSpacer()


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)
        
        self.lab_display = wx.StaticText(self, style = wx.ST_NO_AUTORESIZE | wx.CENTER)
        hbox.Add(self.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.btn_top.Bind(
            wx.EVT_BUTTON,
            lambda event: display_panel.SetLabel("Top")
        )
        control_panel.btn_left.Bind(
            wx.EVT_BUTTON,
            lambda event: display_panel.SetLabel("Left")
        )
        control_panel.btn_right.Bind(
            wx.EVT_BUTTON,
            lambda event: display_panel.SetLabel("Right")
        )
        control_panel.btn_bottom.Bind(
            wx.EVT_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()
mfg
Gerold
:-)
[url]http://halvar.at[/url] | [url=http://halvar.at/elektronik/kleiner_bascom_avr_kurs/]Kleiner Bascom AVR Kurs[/url]
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
sechsrad
User
Beiträge: 173
Registriert: Montag 31. März 2008, 17:09

Donnerstag 8. Mai 2008, 16:22

:roll: ähm...... :cry: hmmmmmm...... :)
Benutzeravatar
gerold
Python-Forum Veteran
Beiträge: 5554
Registriert: Samstag 28. Februar 2004, 22:04
Wohnort: Telfs (Tirol)
Kontaktdaten:

Donnerstag 8. Mai 2008, 16:34

sechsrad hat geschrieben::roll: ähm...... :cry: hmmmmmm...... :)
:twisted: :P
[url]http://halvar.at[/url] | [url=http://halvar.at/elektronik/kleiner_bascom_avr_kurs/]Kleiner Bascom AVR Kurs[/url]
Wissen hat eine wunderbare Eigenschaft: Es verdoppelt sich, wenn man es teilt.
Antworten