Python event & wxPython question:

Tim Hochberg tim.hochberg at ieee.org
Thu Feb 13 23:53:43 CET 2003


bob wrote:
> Here's a piece of my code. My question is this... It's simple, but I
> can't seem to find an example.. On a frame, I have a button, and a
> textbox.. I want to hit a button, and that event will call a function,
> in that function I want the value that was in the textbox.. I can't
> figure out how to access that value in the function that was called...
> Please help.. This is the code, It's just test code, trying to figure
> out some basic funtionality. When the onbtnTest function is called,
> I'd like to have access to the value in txtText ...

This might be better asked on the wxpython list, but here's one way to 
do it. (see below)

Regards,

-tim



> from wxPython.wx import *
> 
> 
> ID_ABOUT = 101
> ID_EXIT  = 102
> ID_BSTUFF = 103
> 
> class MyFrame(wxFrame):
>     def __init__(self, parent, ID, title):
>         wxFrame.__init__(self, parent, ID, title,
>                          wxDefaultPosition, wxSize(400, 350))
>         self.CreateStatusBar()
>         self.SetStatusText("This is the statusbar")
>         menu = wxMenu()
>         menu2 = wxMenu()
> 
>         menu.Append(ID_EXIT, "E&xit", "Terminate the program")
>         menu2.Append(ID_ABOUT, "&About", "More information about this
> program")
>         
>         
>         menuBar = wxMenuBar()
>         menuBar.Append(menu, "&File");
>         menuBar.Append(menu2, "&Help");
>         self.SetMenuBar(menuBar)
> 
>         panel = wxPanel(self, -1)
>         
>         btnTest=wxButton(panel,ID_BSTUFF," Go ")

	  # -----------------------------------------
           # Store a reference to the textCtrl in self
           self.txtText = txtTest = wxTextCtrl(panel, 103, "Write Stuff 
Here!", size=(125,-1)
	  # -----------------------------------------


>         bs=wxBoxSizer(wxHORIZONTAL)
>         bs.Add(20, 0, 0)
>         bs.Add(btnTest,0,wxNORTH ,15)
>         bs.Add(20, 0, 0)
>         bs.Add(txtTest,0,wxNORTH,15)
>         panel.SetAutoLayout(true)
>         panel.SetSizer(bs)
>         EVT_BUTTON(panel,ID_BSTUFF,self.onbtnTest)
>         EVT_MENU(self, ID_ABOUT, self.OnAbout)
>         EVT_MENU(self, ID_EXIT,  self.TimeToQuit)
>         ass = txtTest.GetValue()


      # -----------------------------------------
      def onbtnTest(self,event):
          # Grab the value from the stored reference and print or...
          print self.txtTest.GetValue()
      # -----------------------------------------

> 
>         
>     def OnAbout(self, event):
>         dlg = wxMessageDialog(self, "Some About Stuff",
>                               "About Me", wxOK | wxICON_INFORMATION)
>         dlg.ShowModal()
>         dlg.Destroy()
> 
> 
>     def TimeToQuit(self, event):
>         self.Close(true)
> 
> 
> 
> class MyApp(wxApp):
>     def OnInit(self):
>         frame = MyFrame(NULL, -1, "Hello from wxPython")
>         frame.Show(true)
>         self.SetTopWindow(frame)
>         return true
> 
> app = MyApp(0)
> app.MainLoop()





More information about the Python-list mailing list