-5

Can someone please explain the difference between

import tkinter as tk

and

from tkinter import *

It would be so great if someone could give and example where the same task is achieved (or an object is created) by using these two statements seperately

Berkcem
  • 53
  • 4
  • 1
    @ndmeiri My duplicate is also about tkinter :) – Barmar Jun 14 '18 at 19:49
  • 1
    The man thing you should take from this is that `import tkinter as tk` is the prefered method. This way you prevent your imports from being overridden or overriding other methods in the namespace. – Mike - SMT Jun 14 '18 at 19:56

3 Answers3

1

Simply saying, the import tkinter as tk will initiate a tk instance of tkinter in your file which can call it's functions by writing something like

tk.Entry()

Which will save you from typing the long name.

while from tkinter import * will import all the names defined under the __all__ variable, so you can call the same function as

Entry()

You should read This to understand more

Bhaskar
  • 1,838
  • 1
  • 16
  • 29
0

Both are importing the same package. The primary difference is that your first line is importing the package tkinter and then referring to it with "tk", which would allow you to call its functions using that different name. I don't have any experience with tkinter, but a good example I can give would be with numpy or pandas. A lot of functions in numpy, like numpy.random.randn() could instead be written with a shorthand name using "import as", like so:

import numpy as np
np.random.randn()
Dylan Smith
  • 118
  • 1
  • 10
0

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()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79