2 Replies - 353 Views - Last Post: 14 September 2012 - 06:05 AM Rate Topic: -----

#1 og_the_trog  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-September 12

extracting user info. problem

Posted 13 September 2012 - 09:40 PM

import wx

class build (wx.Dialog):
    
    def __init__ (self,parent,id):
        wx.Dialog.__init__(self,parent,id,"Dictionary", size= (700,500))
        dialog=wx.Dialog(self)
        button = wx.Button(self,label="Search",pos=(460,40),size=(50,20))
        self.Bind(wx.EVT_BUTTON, self.SearchDictionary, button)
        TextBox = wx.TextCtrl (self, -1, "Search Dictionary (Lower-Case)", (250,40), (200,20))
        
    def SearchDictionary (self,event):
        answer = TextBox.GetValue
        if self.answer() == "1":
            print "1"

if __name__=="__main__":
    app=wx.PySimpleApp()
    dialog=build(parent=None,id=-1)
    dialog.Show()
    
    app.MainLoop()


so what happens here is I have a wx.TextCtrl widget with a button in a dialog. What should happen is when you press the button it extracts the word that the user has put into TextCtrl widget but instead I get an error saying that global name 'TextBox' is undefined
ps. I think the reason it's saying that it is undefined is because 'TextBox' is getting defined in a different function so what I want to know is if there's a way to make python know that I am talking about TextBox.

Is This A Good Question/Topic? 0
  • +

Replies To: extracting user info. problem

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5641
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: extracting user info. problem

Posted 14 September 2012 - 03:42 AM

Everything that belongs to the object is referenced with self. You want TextBox to belong to your object, so:
		self.TextBox = wx.TextCtrl (self, -1, "Search Dictionary (Lower-Case)", (250,40), (200,20))

	def SearchDictionary (self,event):
		answer = self.TextBox.GetValue() # don't forget the () for a method call
		print answer


Was This Post Helpful? 0
  • +
  • -

#3 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 317
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: extracting user info. problem

Posted 14 September 2012 - 06:05 AM

Also, did you mean to subclass a wx.Dialog? That is for pop-up... dialogs really. For a main base window use wx.Frame.
class build(wx.Frame) ...



And what does this line do?
dialog=wx.Dialog(self)

You are assigning a wx.Dialog to the variable dialog and then not using it again. Your program will work the same if you remove this line.

This post has been edited by Simown: 14 September 2012 - 06:10 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1