用户在python中单击“确定"时如何保存用户文本框输入? [英] How to save user text box input when user clicks 'ok' in python?

查看:44
本文介绍了用户在python中单击“确定"时如何保存用户文本框输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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!

推荐答案

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

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

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

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)

然后,GetValue 是 TextCtrl 类的一个方法.为了使用它,必须在该类的实例上调用它.

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.

给定您的代码,TextCtrl 仅有的两个(可见)实例是self.howmuch"和self.what".

Given your code, the only two (visible) instances of TextCtrl are "self.howmuch" and "self.what".

这篇关于用户在python中单击“确定"时如何保存用户文本框输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆