请教一个Python函数之间相互调用报错的问题

Python初学者,写了这么一段代码,但报错,希望哪位大神能够帮忙讲解一下,非常感谢ClassA():defGetEntryValues():InputValue=Inp... Python初学者,写了这么一段代码,但报错,希望哪位大神能够帮忙讲解一下,非常感谢
Class A():
def GetEntryValues():
InputValue=InputPath.get()
print InputValue
return InputValue

def OpenInputValue():
print "sdsdsd"
GetEntryValues()

mainFrame=Tk()
frame1=Frame(mainFrame)
frame1.pack()
InputPath=Entry(frame1)
InputPath.pack(side=LEFT)
GoButton=Button(frame1,text='open',command=OpenInputValue)
GoButton.pack(side=LEFT)
mainFrame.mainloop()

在OpenInputValue函数中调用GetEntryValues函数,提示“NameError: global name 'GetEntryValues' is not defined”这是什么原因???
展开
 我来答
老夭来了
2015-03-10 · 知道合伙人软件行家
老夭来了
知道合伙人软件行家
采纳数:1806 获赞数:8100
2008年从事软件开发,拥有多年的python,php,linux工作经验,发布过多个python,php的开源项目。

向TA提问 私信TA
展开全部
Class A(): #class关键字首字母要小写
    def GetEntryValues(): #类中的方法要添加self作为参数,或者使用staticmethod声明成静态函数
        InputValue = InputPath.get()
        print InputValue
        return InputValue


    def OpenInputValue(): #类中的方法要添加self作为参数
        print "sdsdsd"
        GetEntryValues() #调用类中方法的时候,如果是实例方法,需要使用self调用,如果是静态方法,需要类名调用


mainFrame = Tk()
frame1 = Frame(mainFrame)
frame1.pack()
InputPath = Entry(frame1)
InputPath.pack(side=LEFT)
GoButton = Button(frame1, text='open', command=OpenInputValue) #OpenInputValue是类中的方法,需要使用实例或者类名调用
GoButton.pack(side=LEFT)
mainFrame.mainloop()

我觉得你还是应该把python基础好好学习一下。推荐看下python核心编程。


如果解决了您的问题请采纳!
如果未解决请继续追问

追问
按照你说的更改后执行还是报错“TypeError: unbound method OpenInputValue() must be called with Addd instance as first argument (got nothing instead)”,这个是什么意思,而且我把容器创建都写在了类当中,这样写和写在类外面有什么区别么?
追答
既然你对类不是很了解,建议你还是不要使用,直接使用函数,。

你的这个错误我猜测应该是GoButton = Button(frame1, text='open', command=OpenInputValue)

这句你使用类名调用OpenInputValue,也就是A.OpenInputValue, 而定义的时候你把OpenInputValue定义成了实例方法造成的。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式