Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 从函数返回变量_Python 3.x_Oop_Tkinter - Fatal编程技术网

Python 3.x 从函数返回变量

Python 3.x 从函数返回变量,python-3.x,oop,tkinter,Python 3.x,Oop,Tkinter,我有一个主功能页,我用一个按钮链接下一页 当我点击该按钮时,它会在主页面中执行一个函数,但是。 我想使用另一个文件中函数的结果作为变量 ########main.py class Main(object): def __init__(self,master): self.master = master mainFrame = Frame(self.master) mainFrame.pack() topFrame=

我有一个主功能页,我用一个按钮链接下一页 当我点击该按钮时,它会在主页面中执行一个函数,但是。 我想使用另一个文件中函数的结果作为变量


########main.py

class Main(object):
    def __init__(self,master):
        self.master = master
        mainFrame = Frame(self.master)
        mainFrame.pack()


        topFrame= Frame(mainFrame,width=1050,height =50, bg="#f8f8f8",padx =20, relief =SUNKEN,
                        borderwidth=2)
        topFrame.pack(side=TOP,fill = X)

    self.btnselfolder= Button(topFrame, text="Select Folder", compound=LEFT,
                              font="arial 12 bold", command=self.selectFolder)
        self.btnselfolder.pack(side=LEFT)

    def selectFolder(self):
        print("folder")
        return folder

################# selectfolder page


class Page2(Toplevel):

    def __init__(self):
        Toplevel.__init__(self)
        self.geometry("450x300")
        self.title("page2")
        self.resizable(False,False)

        self.topFrame = Frame(self, width=350,height=150, bg="white")
        self.topFrame.pack(fill=X)

    # call the function from main.py and it will give me the same output folder

    y = selectFolder()



由于selectFolder方法不是静态的,因此必须使用instance访问它。像这样:

main = Main()
folder = main.selectFolder()

文件夹
应该保存您返回的值。

当我刚刚将您的代码直接复制并粘贴到我的IDLE中时,它立即向我显示了命名错误。甚至连一扇窗户都没有弹出。所以为了澄清问题,我会制作一个Tkinter窗口,如下所示。我也不知道如何将类与Tkinter本身集成,但我很快就从您的错误中学会了:)


再次感谢你的提问,我非常喜欢解决这个问题,或者至少给你一些指导!)

你在这样做时面临什么问题?
# import tkinter from the libraries
from tkinter import *
import os
# the Zen of Python

#import this


# declare the tkinter window variable
# scroll to bottom to see mainloop
root = Tk()

########main.py

# folder is not defined as a variable
# but i recommend looking into
# using system of which i imported
# for you. You can research how to
# use it yourself for good practice
# i am sorry if i am wrong about this
# but my impression is you're trying to
# use the OS to select a folder from a
# directory. So the 'selectFolder' function
# should not be declared as a method within the class
# therefore rendering the Page2 class useless.


def selectFolder():
    print("folder")

    # return will result in an error
    # because it has not been declared as a variable
    # return folder

class Main():
    # all instances instantiated (created)
    # within the __init__ method
    # must be declared in the parentheses
    def __init__(self, topFrame, btnselfolder):

        # instantiate the topFrame object
        self.topFrame = topFrame

        # instantiate the btnselfolder
        self.btnselfolder = btnselfolder


    # pro tip - when having multiple
    # arguments within an object, then
    # to keep it clean and easy to read
    # space out the code like i have for
    # you. You should also read the "Zen of Python"
    # which is at the top as 'import this'
    # however i have commented it out for you
    topFrame = Frame(root,
                     width=1050,
                     height = 50,
                     bg = "#f8f8f8",
                     padx = 20,
                     relief = SUNKEN,
                     borderwidth=2)

    topFrame.pack(side=TOP, fill = X)

    btnselfolder = Button(topFrame,
                          text = "Select Folder",
                          compound=LEFT,
                          font = "arial 12 bold",
                          command = selectFolder).pack()

# mainloop() method will keep the window open
# so that it doesn't appear for a millisecond
# and dissapear :)
root.mainloop()

# I hope this has been a big help
# Thanks for posting this question! :-)
# Enjoy your day, good luck and be safe in Lockdown!