-1
import tkinter

# what is the difference?
from tkinter import *

What is the difference in two methods? When I used it does not execute the same way.

martineau
  • 112,593
  • 23
  • 157
  • 280
  • lots of similar question, I recommend that you can refer this... [https://stackoverflow.com/questions/43537407/python-whats-the-difference-between-import-x-and-from-x-import](https://stackoverflow.com/questions/43537407/python-whats-the-difference-between-import-x-and-from-x-import) – Mr_U4913 Jun 23 '17 at 23:51

1 Answers1

0

import tkinter is the normal, standard way of importing things. If you use that and you want to use the Frame class from the tkinter module, then you would use variable = tkinter.Frame().

Sometimes, we only need a single thing from a module. If we only need the Frame class, we could use from tkinter import Frame. That way we could use it like this: variable = Frame(). This saves us a tiny bit of typing.

A wildcard import like from tkinter import * imports everything that tkinter offers. So we can again use variable = Frame() and save a bit of typing.

Wildcard imports are used in example code a lot because they make the example shorter and clearer. But you should never use them in real code. They lead to bugs and are against PEP8. You should use a normal import.

There is another trick to save some typing: an alias. The import would be import tkinter as tk, and then you could use it with variable = tk.Frame(). This is the import that you will see most often for tkinter.

From the computer's point of view, all of these imports are exactly the same. None of them is any faster or more efficient than the other. They are all just for the programmers convenience.

Novel
  • 12,855
  • 1
  • 22
  • 38
  • `from tkinter import * windows = Tk() img = PhotoImage("download.png") lable_img = Label(windows, image=img) lable_img.grid() windows.mainloop()` this is code to add image in windows is not work – Mohamed Khaled Jun 24 '17 at 16:29
  • At a glance the only thing I see wrong with that is you didn't specify the file argument. So it should be: `img = PhotoImage(file="download.png")`. This is really a different question and you should make a new post for it. – Novel Jun 26 '17 at 03:23
  • Yes Novel thank you for responding and I'll try this point – Mohamed Khaled Jun 26 '17 at 11:50