The differences seams little at first however import tkinter as tk
is actually a much better option for one big reason.
By importing as something specific you make all the methods from that import required a prifix. This prevents methods from overriding or being overwritten accidentally.
For example if I have a library that contains a time()
method and say lest call this library custom_timer
and then say I need to import the built in time
method also.
If use *
for my import we have a problem.
from custom_timer import * # importing all modules including time() form this library.
import time # importing built in time().
What will end up happening is the 2nd import will override the time method from the first import. To prevent accidents like this we can simple import as your_prefix_here
.
Take this example.
import custom_timer as ct # importing this library as ct.
import time # importing built in time().
In this case we will have 2 distinctive imports for time that do not override each other.
One is ct.time()
and the other is time()
Because libraries like tkinter contain many methods it is safer to use import as
verses import *
for the above reason.
All that said if you are in a hurry and just want to throw something together to test an idea or answer a question using from tkinter import *
is fine for much smaller applications. Personally I try to use import tkinter as tk
for everything I do for good practice.
As for your request for 2 examples see below.
With tk
import:
import tkinter as tk
root = tk.Tk()
tk.Label(root, text="I am a label made using tk prefix.")
root.mainloop()
With *
import:
from tkinter import *
root = Tk()
Label(root, text="I am a label made in tkinter with the * import.")
root.mainloop()