Search code examples
pythonuser-interfaceeventswxpython

Event get value in wxpython


I have a problem with using wxpython. I try to get a value from a column and I want to output to another column too after I hit a button. However, I dont know what I code to add to make this work.

My current code is like this:

import wx

class scarlett(wx.Frame):
  def __init__(self, parent, id, title):
    wx.Frame.__init__(self, parent, id, title, size=(400, 350))

    panel = wx.Panel(self, -1)
    sizer = wx.GridBagSizer(4, 4)

    text1 = wx.StaticText(panel, -1, 'Scarlett')
    sizer.Add(text1, (0, 0), flag = wx.TOP | wx.LEFT | wx.BOTTOM, border=5)
    tc = wx.TextCtrl(panel, -1)
    sizer.Add(tc, (1, 0), (1, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)

    text2 = wx.StaticText(panel, -1, 'You')
    sizer.Add(text2, (2, 0), flag = wx.TOP | wx.LEFT | wx.RIGHT, border = 5 )
    list = wx.ListBox(panel, -1, style = wx.LB_ALWAYS_SB)
    sizer.Add(list, (3, 0), (3, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)

    buttonOk = wx.Button(panel, -1, 'Ok', size=(90, 28))
    buttonCancel = wx.Button(panel, -1, 'Cancel', size=(90, 28))
    sizer.Add(buttonOk, (7, 1))
    sizer.Add(buttonCancel, (7, 2), flag = wx.RIGHT | wx.BOTTOM, border = 5)

    self.Bind(wx.EVT_BUTTON, self.OnQuit, id=buttonCancel.GetId())

    sizer.AddGrowableCol(0)
    sizer.AddGrowableRow(5)
    sizer.AddGrowableRow(6)
    sizer.SetEmptyCellSize((1, 1))
    panel.SetSizer(sizer)

    self.Center()
    self.Show(True)

  def OnQuit(self, event):
    self.Close()


app = wx.App()
scarlett(None, -1, 'scarlett')
app.MainLoop()

Solution

  • If I understand correctly, you are wanting to copy text from the text control to the list box? If that is correct then you simply need to save a reference to the controls so you can access them later in the event handler functions. For example:

    import wx
    
    class scarlett(wx.Frame):
      def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(400, 350))
    
        panel = wx.Panel(self, -1)
        sizer = wx.GridBagSizer(4, 4)
    
        text1 = wx.StaticText(panel, -1, 'Scarlett')
        sizer.Add(text1, (0, 0), flag = wx.TOP | wx.LEFT | wx.BOTTOM, border=5)
        self.tc = wx.TextCtrl(panel, -1)
        sizer.Add(self.tc, (1, 0), (1, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
    
        text2 = wx.StaticText(panel, -1, 'You')
        sizer.Add(text2, (2, 0), flag = wx.TOP | wx.LEFT | wx.RIGHT, border = 5 )
        self.listbox = wx.ListBox(panel, -1, style = wx.LB_ALWAYS_SB)
        sizer.Add(self.listbox, (3, 0), (3, 6), wx.EXPAND |wx.LEFT | wx.RIGHT, 5)
    
        buttonOk = wx.Button(panel, -1, 'Ok', size=(90, 28))
        buttonCancel = wx.Button(panel, -1, 'Cancel', size=(90, 28))
        sizer.Add(buttonOk, (7, 1))
        sizer.Add(buttonCancel, (7, 2), flag = wx.RIGHT | wx.BOTTOM, border = 5)
    
        self.Bind(wx.EVT_BUTTON, self.OnQuit, buttonCancel)
        self.Bind(wx.EVT_BUTTON, self.OnCopyText, buttonOk)
    
        sizer.AddGrowableCol(0)
        sizer.AddGrowableRow(5)
        sizer.AddGrowableRow(6)
        sizer.SetEmptyCellSize((1, 1))
        panel.SetSizer(sizer)
    
        self.Center()
        self.Show(True)
    
      def OnQuit(self, event):
        self.Close()
    
      def OnCopyText(self, event):
        self.listbox.Append(self.tc.GetValue())
    
    app = wx.App()
    frm = scarlett(None, -1, "scarlett")
    app.MainLoop()