self.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus, self.rtc)?

Plattformunabhängige GUIs mit wxWidgets.
Antworten
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

Eigentlich sollte mein Titel länger werden.

Titel: self.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus, self.rtc); self.rtc.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus)


Mir ist etwas komisches aufgefallen:

Normalerweise bindet man ja Events per self.Bind(evt, handler, source, id1, id2)

das hat bei mir aber nicht funktioniert und ich musste: self.(source).Bind(evt, handler) verwenden, siehe Besipiel

Code: Alles auswählen

import wx
import wx.richtext as rt

class NotebookPages(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id)

        vbox = wx.BoxSizer(wx.VERTICAL)        
        
        hbox1 = wx.BoxSizer(wx.HORIZONTAL)
        # more hboxes
                        
        self.rtc = rt.RichTextCtrl(self, style=wx.HSCROLL | rt.RE_READONLY | rt.TEXT_ATTR_URL)
        hbox1.Add(self.rtc, 5, wx.EXPAND | wx.ALL, 10)
        vbox.Add(hbox1, 1, wx.EXPAND)
        
        # more GUI Layout
        
        self.SetSizer(vbox)
        
        self.rtc.Bind(wx.EVT_KILL_FOCUS, self.on_kill_focus)
    
    def on_kill_focus(self, event):
        # do something!
        pass
Kann mir jemand erklären wieso ich das Event so "komisch" binden muss?
the more they change the more they stay the same
IoI
User
Beiträge: 68
Registriert: Dienstag 1. Dezember 2009, 11:39

Komisch finde ich das nicht, man bekommt eben mehrere Möglichkeiten einen Bind zu erstellen. Das kann ganz praktisch sein, wenn man z.B. -1 als ID verwendet.

Beispiel:

Code: Alles auswählen

btn = wx.Button(self, -1, "Hallo Welt")
btn.Bind(wx.EVT_BUTTON, self.OnHallo)
ist nichts anderes als

Code: Alles auswählen

HALLOBTN = wx.NewId()
btn = wx.Button(self, HALLOBTN, "Hallo Welt")
self.Bind(wx.EVT_BUTTON, self.OnHallo, HALLOBTN)
Genauso könntest du deine rt.RichTextCtrl über eine ID ansteuern, wenn du eine zugewiesen hättest.
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

Oh ja, da hab ich was durcheinander gebracht ID und "Rtc"
the more they change the more they stay the same
Antworten