【问题标题】:How to save user text box input when user clicks 'ok' in python?当用户在python中单击“确定”时如何保存用户文本框输入?
【发布时间】:2014-07-18 21:24:04
【问题描述】:

我正在尝试制作一个接受两个文本输入的弹出窗口,然后当用户单击“确定”时,它会记录数据。我的问题是当我尝试定义按下“确定”按钮时的功能时,记录数据。当我按下确定时,我得到了AttributeError: 'apples' object has no attribute 'TextCtrlInstance'

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()

我希望这足以给我一个解决方案!谢谢,我期待答案!

【问题讨论】:

  • 我对 wx 不熟悉,但是我没有看到 TextCtrlInstance 是在哪里定义的。
  • 在 Python 中有一种用法,即 类名 具有大写首字母,而 实例名 以小写开头。像所有的编码约定一样,这也是有争议的。但是,我建议您暂时坚持使用这种用法,因为这可能有助于您区分代码中的 对象

标签: python python-2.7 wxpython


【解决方案1】:

一个受过教育的猜测,因为我没有运行你的代码:

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

如果您需要从同一对象的其他方法访问它们,则需要将各种小部件存储为 instance 变量。在 Python 中,这是写成self.varname = ....。通常来自__init__ 特殊方法。你可能错过了很多(也许不是我添加的所有这些——YMMV)

那么,GetValue 是 TextCtrl 类的一个方法。为了使用它,必须在该类的 instance 上调用它。

根据您的代码,TextCtrl 仅有的两个(可见)实例是“self.howmuch”和“self.what”。

【讨论】:

  • 这不起作用,因为我收到了 AttributeError: 'apples' object has no attribute 'howmuch' 错误。似乎我必须以某种方式获取 init 值?
  • init 函数中执行 self.what=... 而不是 what=...
  • 感谢@Dave 的评论:我不知道我怎么会错过那个 o_O !
  • 感谢你们!我可以知道用我的基本程序做其他事情!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多