简体   繁体   中英

Importing stuff from tkinter Python

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM