问题
I am trying to make a pop up window that takes two text inputs and then when the user clicks 'ok', it records the data. My problem is when I try to define the function of when the 'ok' button is pressed, record the data. I get the AttributeError: 'apples' object has no attribute 'TextCtrlInstance'
when I press ok.
class apples(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Add a stock',size=(300,300))
frames=wx.Panel(self)
frames.Bind(wx.EVT_MOTION, self.OnMove)
frames.Bind(wx.EVT_MOTION, self.count)
howmuch=wx.TextCtrl(frames,-1,'#of',pos=(200,173))
cancel=wx.Button(frames,label='Cancel',pos=(100,250),size=(60,40))
self.Bind(wx.EVT_BUTTON, self.ca, cancel)
wx.StaticText(frames,-1,'Enter in valid stock ticker:',pos=(10,50))
what=wx.TextCtrl(frames,-1,'AAPL',pos=(200,48))
okbutton = wx.Button(frames,label='OK',pos=(200,250),size=(60,40))
self.Bind(wx.EVT_BUTTON,self.oker,okbutton)
wx.StaticText(frames,-1,'Enter in nuber of shares:',pos=(10,175))
def ca(self,event):
self.Destroy()
def oker(self,event):
#I need the user info when they press ok
print 'Saved!'
self.TextCtrlInstance.GetValue()
self.Destroy()
def OnMove(self,event):
pass
def count(self,event):
pass
if __name__ =='__main__':
apps = wx.PySimpleApp()
windows = apples(parent=None,id=-1)
windows.Show()
apps.MainLoop()
I hope this is enough to give me a solution! Thanks and I look forward to the answers!
回答1:
An educated guess as I didn't run your code:
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'Add a stock',size=(300,300))
self.frames=wx.Panel(self)
self.frames.Bind(wx.EVT_MOTION, self.OnMove)
self.frames.Bind(wx.EVT_MOTION, self.count)
self.howmuch=wx.TextCtrl(frames,-1,'#of',pos=(200,173))
self.cancel=wx.Button(frames,label='Cancel',pos=(100,250),size=(60,40))
self.Bind(wx.EVT_BUTTON, self.ca, cancel)
wx.StaticText(frames,-1,'Enter in valid stock ticker:',pos=(10,50))
self.what=wx.TextCtrl(frames,-1,'AAPL',pos=(200,48))
self.okbutton = wx.Button(frames,label='OK',pos=(200,250),size=(60,40))
self.Bind(wx.EVT_BUTTON,self.oker,okbutton)
wx.StaticText(frames,-1,'Enter in nuber of shares:',pos=(10,175))
[...]
def oker(self,event):
qty = self.howmuch.GetValue()
what = self.what.GetValue()
print "Saved", qty, "of", what
You need to store your various widget as instance variables if you need to access them from other methods of the same object. In Python, this is written self.varname = ....
. Usually from the __init__
special method. You probably missed a bunch of them (maybe not all those I've add -- YMMV)
Then, GetValue is a method of the TextCtrl class. In order to use it, it has to be invoked on an instance of that class.
Given your code, the only two (visible) instances of TextCtrl
are "self.howmuch
" and "self.what
".
来源:https://stackoverflow.com/questions/24834010/how-to-save-user-text-box-input-when-user-clicks-ok-in-python