wxpython 在事件处理程序中分配变量



我想将菜单单击的值分配给类变量,以便我可以在多个类中使用该值。 任何帮助将不胜感激!

import wx
class TrackPanel (wx.Panel):
    """"""
    def __init__(self, parent, *args, **kwargs):
        """Constructor"""
        wx.Panel.__init__(self, parent, *args, **kwargs)
        mainSizer =wx.BoxSizer(wx.VERTICAL)
        track = MasterPage.track_selected  # This is where i would like access the variable      
        title = wx.StaticText(self, wx.ID_ANY, label = track)
        mainSizer.Add(title, 0,wx.ALL | wx.EXPAND | wx.CENTER)
        self.SetSizerAndFit(mainSizer)
        self.Layout()
class MasterPage (wx.Frame):
    track_selected = ' '   
    def __init__(self, *args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        menubar = wx.MenuBar()
        tracksopt = wx.Menu()
        track_command = wx.MenuItem(tracksopt, wx.ID_ANY, 'Atlanta')
        tracksopt.Append(track_command)
        self.Bind(wx.EVT_MENU, self.change_track, track_command)
        track_command2 = wx.MenuItem(tracksopt, wx.ID_ANY, 'Texas')
        tracksopt.Append(track_command2)
        self.Bind(wx.EVT_MENU, self.change_track, track_command2)        
        menubar.Append(tracksopt, '&Tracks')
        self.SetMenuBar(menubar)                       
    def change_track (self, event):
        """Changes the tack_selected variable based on the menu item picked"""
        id_selected = event.GetId()
        obj = event.GetEventObject()
        track_id= obj.GetLabel(id_selected)
        MasterPage.track_selected = track_id

您必须在跟踪器中创建指向主控形状的链接,以便跟踪器访问主控形状中的值。

import wx
class Track (wx.Frame):
    def __init__(self, link):
        wx.Frame.__init__(self, None)
        self.link = link
        self.tc = wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.bt = wx.Button(self, label="tracker (push to read master)")
        self.bt.Bind(wx.EVT_BUTTON, self.on_read_master)
        self.Title = 'Tracker'
        sz_1 = wx.BoxSizer(wx.HORIZONTAL)
        sz_2 = wx.BoxSizer(wx.VERTICAL)
        sz_2.Add(self.tc, 1, wx.EXPAND, 0)
        sz_2.Add(self.bt, 0, wx.EXPAND, 0)
        sz_1.Add(sz_2, 1, wx.EXPAND, 0)
        self.SetSizer(sz_1)
        self.Layout()
        self.SetSize((250, 100))
    def on_read_master(self, evt):
        self.tc.SetValue(self.link.track_selected)   # here you access the variable)
class Master(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        self.track_selected = '0'
        self.tc = wx.TextCtrl(self, wx.ID_ANY, "", style=wx.TE_MULTILINE)
        self.bt = wx.Button(self, wx.ID_ANY, "master (push to send)")
        self.bt.Bind(wx.EVT_BUTTON, self.change_track)
        self.Title = 'Master'
        self.tc.SetValue('enter value to be read by the tracker')
        sz_1 = wx.BoxSizer(wx.HORIZONTAL)
        sz_2 = wx.BoxSizer(wx.VERTICAL)
        sz_2.Add(self.tc, 1, wx.EXPAND, 0)
        sz_2.Add(self.bt, 0, wx.EXPAND, 0)
        sz_1.Add(sz_2, 1, wx.EXPAND, 0)
        self.SetSizer(sz_1)
        self.Layout()
        self.SetSize((250, 100))
    def change_track (self, evt):
        """Changes variable based on text entered"""
        self.track_selected = self.tc.GetValue()
if __name__ == "__main__":
    app = wx.App(0)
    master =  Master()
    track = Track(master)
    master.Show()
    track.Show()
    app.MainLoop()

这是我通常使用的快速而肮脏的方法。如果你想要更复杂的东西,有一个专门的库可以在帧之间传递消息:pubsub。有关如何使用它的好教程可以在Mike Driscoll博客中找到

相关内容

  • 没有找到相关文章