(from tkinter import *) vs (import tkinter) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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ā€?

23rd Oct 2019, 8:16 PM
Hind
Hind - avatar
2 Answers
+ 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. šŸ˜‚
23rd Oct 2019, 8:51 PM
HonFu
HonFu - avatar
+ 1
Thank you for your help. Itā€™s much clearer now. šŸ‘šŸ»
23rd Oct 2019, 9:40 PM
Hind
Hind - avatar