< wxPython GUI

wxPython - Üzenetküldés a komponensek között

Ablak nyomógombbal

Korábban a wx.lib.pubsub.pub volt használatos. Ennek használata elavult. Használjuk helyette a pubsub.sub-t.

Használat előtt telepíteni kell:

pip3 install pypubsub
pubsub_proba.py
import wx
from pubsub import pub
 
class SecondFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        self.entry1 = wx.TextCtrl(self, size=(120, 30))
        self.close_button = wx.Button(self, label='Bazár')
        self.close_button.Bind(wx.EVT_BUTTON, self.on_click_close_button)
        pub.subscribe(self.uzenet_figyelo, 'uzeno')
 
        main_box = wx.BoxSizer(wx.VERTICAL)
        main_box.Add(self.entry1)
        main_box.Add(self.close_button)
        self.SetSizer(main_box)
        self.Fit()
 
    def uzenet_figyelo(self, uzenet):
        self.entry1.SetValue(uzenet)
 
    def on_click_close_button(self, event):
        self.Close()
 
class MainFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
 
        self.open_button = wx.Button(self, label='Mutat')
        self.open_button.Bind(wx.EVT_BUTTON, self.on_click_open_button)
        self.send_button = wx.Button(self, label='Küld')
        self.send_button.Bind(wx.EVT_BUTTON, self.on_click_send_button)
 
        main_box = wx.BoxSizer(wx.VERTICAL)
        main_box.Add(self.open_button)
        main_box.Add(self.send_button)
        self.SetSizer(main_box)
 
    def on_click_open_button(self, event):
        secondFrame = SecondFrame(self, wx.ID_ANY, 'második')
        secondFrame.Show()
 
    def on_click_send_button(self, event):
        pub.sendMessage('uzeno', uzenet='ez megy át')
 
class SimpleApp(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True
 
if __name__ == "__main__":
    app = SimpleApp(0)
    app.MainLoop()