-2

I can't figure out how to import a function from another python file and place it into a button function. This is the code I've written personally:

def press_btn1():
    text = entry1.get()
    if text == "something":
        test()
        return None
2
  • oh, also the test() function is in ananother file (also .py) Commented Aug 10, 2021 at 16:27
  • from myotherfile import test should do it. Note: from myotherfile not from myotherfile.py. Commented Aug 10, 2021 at 16:30

2 Answers 2

0

Imagine that you have two .py files main_file and sub_file

So in sub_file, I have a function called print_hello() that prints hello.

This is in sub_file:

def print_hello():
    print('hello')

So to access this function in main_file you have to import sub_file and call the function in the sub_file:

This is in main_file:

import sub_file

sub_file.print_hello()

so this gives the output as hello

Now you can use this concept in you code

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

Comments

0

I am assuming the below scenario:

In File also.py , you have test function:

def test():
 ...
 ...

In your main (assuming main.py), where you are using test() :

import also as a
..
..
..
def press_btn1():
    text = entry1.get()
    if text == "something":
        a.test()
        return None

MAKE SURE the also.py and main.py are in the same directory

If they are in the different directory, then read this

1 Comment

thanks a lot! Its my First day of stackoverflow and THIS was SO HELPFUL, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.