1

I have a main function page and i linked the next page with a button when i click the button it executes a function in the main page but. i want to use the result from the function in another file as a variable


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


1
  • What problem are you facing in doing so? Commented Apr 22, 2020 at 15:12

2 Answers 2

2

Since the selectFolder method is not static, you'll have to access it using instance. Like this:

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

folder should hold the value you returned.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm afraid when i just copied and pasted your code into my IDLE directly, it immediately presented me with naming errors. Not even a Tkinter window popped up. So to clear things up I would go about making a Tkinter window like the following. Also bare in mind I don't really use or know how to integrate classes with Tkinter itself, but I quickly learned how to by your mistakes :)

# 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!

Thanks again for the question, I thoroughly enjoyed solving it or at least giving you some guidence! :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.