Python Forum
What Does from tkinter import filedialog Mean?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What Does from tkinter import filedialog Mean?
#1
Hello all

New to python.

I just wanted to ask what does the following mean:-

import tkinter 
Would it be correct to say that this code means that i am importing the tkinder module and all the associated functions contained within that module so that i can use them in my python code. Hopefully my understanding is correct.

I have also seen the following code:-

from tkinter import filedialog
I dont fully understand what this code does, am i only importing the filedialog functions from the tkinter module?, wouldn't it be better to just import everything using the import tkinter command?

Can anyone explain?

Thank you.
Reply
#2
(Aug-06-2020, 09:30 PM)JoeDainton123 Wrote: I dont fully understand what this code does, am i only importing the filedialog functions from the tkinter module?, wouldn't it be better to just import everything using the import tkinter command?
They may have added at a later point,then deiced to just make it own import.
So now it's only import filedialog.py from root_python\Lib\tkinter.
Yes is would have been better if all was under import tkinter,have talk about this many time in package Threads.
This is not so bad i have seen much in worse package design,with many an horrible long imports.
To pick a little on Matplotlib Doh
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
Reply
#3
import adds things to the namespace. When Python encounters a word that isn't a Python keyword (for, while, if, else...) it looks for the word in the namespace. If the word is not in the namespace Python raises a NameError.
root = tkinter.Tk()
Output:
Traceback (most recent call last): File "C:\python_musings\junk.py", line 1, in <module> root = tkinter.Tk() NameError: name 'tkinter' is not defined
But if I import tkinter Python can find 'tkinter' in the namespace and find "things" that are defined by the tkinter module.
import tkinter
print(tkinter)
root = tkinter.Tk()  # uses tkinter module to find tkinter.Tk()
Output:
<module 'tkinter' from 'C:\\Program Files\\Python38\\lib\\tkinter\\__init__.py'>
If you don't import any modules your program is limited to using Python keywords and a few classes. You can use dir() to get a list of everything in the current namespace:
for name in dir():
    print(name)
Output:
__annotations__ __builtins__ __doc__ __file__ __loader__ __name__ __package__ __spec__
Doesn't look like much, but if you start looking at the classes you'll find that each adds a lot of abilities to Python.
for name in dir(__builtins__):
    print(name)
Output:
ArithmeticError AssertionError AttributeError BaseException BlockingIOError BrokenPipeError BufferError BytesWarning ChildProcessError ConnectionAbortedError ConnectionError ConnectionRefusedError ConnectionResetError DeprecationWarning EOFError Ellipsis EnvironmentError Exception False ...
from tkinter import Tk adds Tk to the namespace.
from tkinter import Tk
for name in dir():
    print(name)
Output:
Tk __annotations__ __builtins__ __doc__ __file__ __loader__ __name__ __package__ __spec__
Now I can use Tk in my program because 'Tk' is defined in the namespace. Notice however that tkinter is not defined in the namespace and using tkinter.Tk() raises a NameError exception.
from tkinter import Tk
root1 = Tk()
root2 = tkinter.Tk()
Output:
Traceback (most recent call last): File "C:\python_musings\junk.py", line 3, in <module> root2 = tkinter.Tk() NameError: name 'tkinter' is not defined
In addition to adding things to the namespace, import executes the code in the imported module. I have two files; junk.py and junk2.py
junk2.py
def printit(a):
    print('printing ', a)
printit('Hello world')
junk.py
import junk2
junk2.printit('Hi Mom!')
When I execute the command python junk.py this is the output:
Output:
printing Hello world printing Hi Mom!
Not only does this program execute the printit() call inside junk.py, it also executes the printit() call inside junk2 when junk2 is imported.

Sometimes you want to execute code in an imported module, but sometimes you only want to execute code if the module is the "main" file, the file provide to the python command. This is why at the bottom of many Python modules (files) you will see this:
if __name__ == '__main__':
    # conditionally executed code goes here
This if statement only executes when the file is being run by Python as the main file, not when the file is imported.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020