Skip to content
Advertisement

If I created widgets in one function, how can I access them in another function using Python Tkinter?

this is my first project using Tkinter so please excuse me if the question is very simple to solve. Depending on what option the user chooses from a dropdown, I call a function that creates and places certain widgets (e.g. an entry) on a frame. Then, when another button is pushed I want to access the text inside this entry. However, this seems to be giving me errors (saying that the widget is undefined) because I want to access a widget which I create when a function is called.

An obvious solution I see is to create all possible widgets I want to use outside the function, and only place them when the function is called. This seems quite sloppy and creates many more issues. Is there another fix?

Thanks in advance!

This is the function where I create and place the widgets on the frame.

JavaScript

This is a snippet of a function which uses input from the entry widget I created above:

JavaScript

When the second function executes, I get a runtime error that entryAuthSur1 is not defined.

Advertisement

Answer

All variables inside functions are local. That means that it is deleted after the function call ends. As your variable (entryAuth1) wasn’t global so it only existed inside the function and was deleted when the loadBook function ended. This is the working code:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement