+ 2
(from tkinter import *) vs (import tkinter)
What is the difference between “from tkinter import *” and “import tkinter”? Why doesn’t it work when I use “import tkinter”?
2 Respuestas
+ 9
Like with all the other modules, the difference is this:
In variation 1 you'll have all the names of tkinter in your code and can use them as they are.
In variation 2 you have only the module imported as a name, which means that you'll have to write tkinter.this and tkinter.that etc.
The problem with the star syntax is that all the names from the module end up in your code, and if you already have similar names in there, they will be overwritten.
Dumb example:
You load a file and call it 'text'.
Now in tkinter there is a variable 'text' too (I'm making this up). You don't know it, and write:
from tkinter import *
Now your text is overwritten, but you don't know it, so you save 'your' text.
What happens?
You have just overwritten your file with something else.
So, as always, this is about if you know what you're doing, or not.
In case of doubt, 'import tkinter' is the safer way.
At least if you haven't loaded your file and called it tkinter before. 😂
+ 1
Thank you for your help. It’s much clearer now. 👍🏻