from tkinter import * vs import tkinter in Python
Let’s learn whats the difference is between using from tkinter import *
and import tkinter as tk
.
Both of those will work fine for your program.
If you use from tkinter import * then all the methods and functions that is in tkinter package will be imported to the global namespace of your program. To make it simple, just remember that it will load all the methods and functions directly to your program and it will occupy the memory as well.
So you do not have to use class name or object name before the functions of tkinter.
For example, you can write this:
from tkinter import * newWindow = Tk() newWindow.mainloop()
It will work fine. You can see I am using Tk() function directly on the second line.
But if you do the same with import tkinter as tk then you will have to write it like this:
import tkinter as tk newWindow = tk.Tk() newWindow.mainloop()
Its always a good practice not to use from package_name import *. Always try to use import package_name. (I am not only talking about tkinter here)
The reason is if you use from tkinter import *
, there’s a huge drawback.
For example, if you are using any other package or library in the same program and that package is using a function which is named exactly the same. Then you will encounter a name conflict and you will not be getting your expected output.
I hope this information is helpful to you.
Leave a Reply