需要返回父函数python的值

2024-04-25 20:47:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在macosx上使用python2.7.5

在我的代码中,有一个函数需要返回一个值。在这个函数中,我希望我的回调函数(tkinter)使它的父函数返回一个值,但是如果有更好的方法,请说。抱歉,如果我不清楚,因为我不知道如何解释我的问题。在

def Open():
    class dataC:
        lines=[]
    data=dataC


    def event():
        path=pathE.get()
        file=fileE.get()

        try:
            os.chdir(path)

            temp=open(file)
            lines=temp.readlines()
            temp.close()
        except:
            lines=[]
        else:
            os.chdir(path)

            temp=open(file)
            lines=temp.readlines()
            temp.close()

        data.lines=lines


    gui=Tk()
    gui.title("Open File")

    pathE=Entry(gui)
    pathE.pack(padx=5,pady=5)
    pathE.insert(INSERT,startingDir)

    fileE=Entry(gui)
    fileE.pack(padx=5,pady=5)
    fileE.insert(INSERT,"File Name")

    openE=Button(gui,text="Open File",command=event)
    openE.pack(padx=5,pady=5)
    gui.mainloop()

Tags: path函数defguiopentemppackfile
1条回答
网友
1楼 · 发布于 2024-04-25 20:47:20

{1{I希望在单击循环时尽快返回一个值。所以你必须加上一行

gui.quit()

或者

^{pr2}$

event()中。在event()中时,可以将要返回的值保存在Open()的局部变量中,就像您已经对data所做的那样,然后在mainloop行之后返回它。在

相关问题 更多 >