-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
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437

2 Answers2

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

Johan Jomy
  • 592
  • 2
  • 7
  • 22
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

Shubhankar
  • 72
  • 1
  • 10