0

I am having a few issues, calling Python functions defined in another script using tkinter. I would prefer to have a separate script for my functions that the GUI uses when needed. At the moment I am doing it like this.

ttk.Button(mainframe, text="1", command=one).grid(column=1, row=1, sticky=NW)

def one():
    code_entry.insert(END,"1")

The above calls the command one on a button click, which will print the character one in a entry field with the GUI. I thought I could create a separate script to hold my functions and call them like this:

ttk.Button(mainframe, text="1", command=functions.one()).grid(column=1, row=1, sticky=NW)

And then simply add an import statement at the top of my GUI, like below:

import functions

This doesn't work and looking for some advice on how to approach this.

1 Answer 1

1

You didn't specify any error messages, but it's most likely that you're doing fuctions.one() - actually calling the one() function of that module before the Button is created. It's simply fixed by removing the () part - when you specify a function without (), you are passing a reference of the function object.

Also keep in mind the scope of the code_entry variable - if you were using it as a module level global before (or function local, if one() was inside the same function as your ttk.Button call), it won't be available when you move it to a new namespace without code_entry.

To solve this you should pass code_entry as a parameter to the callback without calling one() at first. The usual approach for this is creating a lambda - essentially creating a function that works on the same scope of the original one(), having access to variables like code_entry, but also calling a function in a different module.

ttk.Button(mainframe, text="1", command=lambda: functions.one(code_entry))

Note that this is basically the same as:

def some_anonymous_function():
    functions.one(code_entry)
ttk.Button(mainframe, text="1", command=some_anonymous_function)

Both examples create a function object and pass that object as reference - the functions.one() call of the lambda is actually inside the body of the lambda function, to be called later by tkinter.

Of course you also have to redefine one() to accept this new parameter:

def one(code_entry):
    code_entry.insert(END,"1")
Sign up to request clarification or add additional context in comments.

1 Comment

@dequis what should I do if I want to pass some value to the function? I have a paint app in which I've written the save function on another script. for the save function to work it needs the tkinter canvas, I tried to pass the canvas to the function by save.bind('<ButtonPress-1>', export.save_img(canvas)) it does not work, but this works if the save function is in the same script save.bind('<ButtonPress-1>', export.save_img) please help me, because of this I couldn't have any function that needs arguments outside the main file.

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.