Why use Python code "from tkinter import ttk"? [duplicate] - python

This question already has answers here:
Python: `from x import *` not importing everything
(3 answers)
Closed 6 years ago.
Python applications using TK GUIs usually call the following import statements:
from tkinter import *
from tkinter import ttk
Isn't calling from tkinter import ttk redundant? I thought ttk would already be imported by calling from tkinter import *
Please explain why ttk needs to be imported separately?
(I already understand that it has "improved widgets". I want to understand why the improved widgets aren't accessible from the from tkinter import * call.)

If you go to your python installed location, you will find that in the python library, tkinter is a folder instead of a .py file. So when you use from tkinter import *, you actually only imported things in your-python-location/lib/tkinter/__init__.py. Things like ttk are actually separate files in tkinter folder(e.g. lib/tkinter/ttk.py, lib/tkinter/scrolledtext.py etc.) Therefore, from tkinter import * and from tkinter import tkk are different commands that import things from different module.

Related

Import tkinter/Tkinter as "Tk" or "tk"?

I've seen the light an will now convert all my tkinter references from Canvas to Tk.Canvas, from Label to tk.Label, and from root = Tk() to root = tk.Tk(), etc.
Stuck in between Python 2.7.12 and Python 3.5 my program stub looks like this:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
import tkinter as Tk
import tkinter.ttk as ttk
import tkinter.font as font
except ImportError: # Python 2
import Tkinter as Tk
import ttk
import tkFont as font
The question is should I be using import tkinter as Tk or import tkinter as tk?
I'm thinking the latter but I would like to stick to industry standards. Also take into consideration how most answers are written in Stack Overflow. Going with this majority means I can try code snippets with the least modifications.
The advantage I see using import tkinter as tk is:
Tkk is imported as ttk so there is capitalization consistency.
tkFont is imported as font so there is capitalization consistency.
I'm still on my first Python project and want to develop good reusable code for future projects.
The question is should I be using import tkinter as Tk or import tkinter as tk?
You should use import tkinter as tk, based on the naming conventions in pep8

Why import duplicates from a module [duplicate]

This question already has an answer here:
Why use Python code "from tkinter import ttk"? [duplicate]
(1 answer)
Closed 4 years ago.
I am learning tkinter and noticed that people import multiple things sometimes.
from tkinter import *
from tkinter import ttk
I was wondering why people do this for many modules, not just tkinter. I always thought that import * meant that you were importing everything from a module. So why do people import more items?
tkinter.ttk is a submodule of tkinter. Submodules aren't guaranteed to be loaded by import *; if you want them, it's safest to import them yourself. (Whether submodules are loaded by import * depends on the presence and contents of an __all__ list, as well as what imports __init__.py performs and what imports have already been performed.)

Tkinter cannot find filedialog? [duplicate]

This question already has answers here:
Tkinter import filedialog error
(4 answers)
Closed 5 years ago.
I'm having some issues using tkinter. To start, I'm using Python 2, and I've seen people on SO use a mixture of import tkinter and import Tkinter; only the latter works for me, although there is another module names _tkinter. Which am I supposed to use here?
The real issue is that tkinter cannot find filedialog. I'm trying to do path = Tkinter.filedialog.askopenfilename() but I'm getting an error AttributeError: 'module' object has no attribute 'filedialog'. What can I do about this?
Python 2's tkinter and Python 3's tkinter libraries have some differences.
Python 2:
import Tkinter
Python 3:
import tkinter
Python 2:
import tkFileDialog # ( `as filedialog` ) to import the filedialog module
Python 3:
from tkinter import filedialog
You have to use the Python 2 code samples. Every method call to tkinter should be the same in both Python versions. However you have to import different modules. For any further questions just search the wiki for your Python version (Python 2) or feel free to ask a new question here on Stack Overflow.

why does tkinter ttk showing "name ttk is not defined" in python 3.5.1

Consider this simple code:
from tkinter import *
from tkinter.ttk import *
root= Tk()
ttk.Label(root, text='Heading Here').grid(row=1, column=1)
ttk.Separator(root,orient=HORIZONTAL).grid(row=2, columnspan=5)
root.mainloop()
when i run this code it's showing error
ttk.Label(root, text='Heading Here').grid(row=1, column=1)
NameError: name 'ttk' is not defined
When you do import X, you are importing a module named X. From this point on, X will be defined.
When you do from X import *, you are not importing X, you are only importing the things that are inside of X. X itself will be undefined.
Thus, when you do from tkinter.ttk import *, you are not importing ttk, you are only importing the things in ttk. This will import things such as Label, Button, etc, but not ttk itself.
The proper way to import ttk in python3 is with the following statement:
from tkinter import ttk
With that, you can reference the ttk label with ttk.Label, the ttk button as ttk.Button, etc.
Note: doing from tkinter.ttk import * is dangerous. Unfortunately, ttk and tkinter both export classes with the same name. If you do both from tkinter import * and from tkinter.ttk import *, you will be overriding one class with another. The order of the imports will change how your code behaves.
For this reason -- particularly in the case of tkinter and ttk which each have several classes that overlap -- wildcard imports should be avoided. PEP8, the official python style guide, officially discourages wildcard imports:
Wildcard imports ( from import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools.
Note: your question implies you're using python 3, but in case you're using python 2 you can just do import ttk rather than from tkinter import ttk. ttk moved in python 3.
To import ttk, replace the following line:
from tkinter.ttk import *
with:
from tkinter import ttk
Otherwise, attributes of tkinter.ttk module will be loaded into the current module namespace instead of ttk itself.
When you are importing the ttk module, you can do it in 2 ways -
from tkinter import ttk
When you do this, ttk is imported almost like a variable, so you can use that ttk.Label
from tkinter import *
This is called wildcard import. You can't use ttk.Label you have to directly write Label(options)
ttk.Label(root, text='HeadingHere').grid(row=1, column=1)
NameError: name 'ttk' is not defined
In this remove ttk as follows.
Label(root, text='HeadingHere').grid(row=1, column=1
Now it works fine

Python tkinter 8.5 import messagebox

The following code runs fine within IDLE, but otherwise I get "NameError: global name 'messagebox' is not defined". However, if I explicitly state from tkinter import messagebox, it runs fine from where ever.
from tkinter import *
from tkinter import ttk
root = Tk()
mainFrame = ttk.Frame(root)
messagebox.showinfo("My title", "My message", icon="warning", parent=mainFrame)
Why does IDLE not need the explicit import statement but elsewhere it is required?
the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:
from tkinter import *
doesn't import messagebox
it has to be explicitly imported like so:
from tkinter import messagebox
in the same way that ttk has to be imported explicitly
the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle
IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.
messagebox.showinfo is defined inside tkinter/showinfo.py but when you use from tkinter import * you only import tkinter/__init__.py which holds the definitions of Label, Entry, Button, ... That is how python imports work.
When you use from tkinter import messagebox it looks for messagebox inside tkinter/__init__.py but it can't find it so it tries to import tkinter/messagebox.py
As for the IDLE anomaly, it is a bug in IDLE and I believe that it was patched.

Categories