0

I am new to Python Programming. I am using tkinter module to import stuff for graphical user interface.

I import everything from the tkinter module by typing the following statement:

from tkinter import *

Why do I still need to import some certain classes separately from the same module like simpledialog although we have imported everything using the above statement from tkinter?

from tkinter import simpledialog

Without importing simpledialog separately, it throws an error.

Nouman
  • 6,947
  • 7
  • 32
  • 60
  • Have you checked out: https://stackoverflow.com/questions/3615125/should-wildcard-import-be-avoided ? Are you looking for advice about object oriented programming in Python? – Charles Landau Oct 30 '18 at 05:40

1 Answers1

3

Yes, it does throw the error because simpledialog is not directly the module of tkinter.

Basically, the module is every file, which has the file extension .py and consists of proper Python code. There is no special syntax required to make such a file a module. A module can contain arbitrary objects, for example files, classes or attributes. All those objects can be accessed after an import.

If you will do dir(tkinter) without importing the simpledialog explicitly, it shows the results except simpledialog in it.

This is because of the library structure. It doesn't import all the modules of tkinter automatically. Once you use import tkinter.simpledialog it will show you the simpledialog module in it, which means simpledialog was never imported from tkinter earlier.

Also, it is recommended to use import tkinter instead of from tkinter import astrik, except when working in the interactive Python shell. One reason is that the origin of a name can be quite obscure, because it can't be seen from which module it might have been imported.

Pang
  • 9,564
  • 146
  • 81
  • 122
Raghavendra Gupta
  • 355
  • 1
  • 2
  • 15