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

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

Related

Why can't I access tkinter objects after importing tkinter as something?

I'm following a simple tutorial, learning about the tkinter library. In the tutorial, they do this:
from tkinter import *
from tkinter import ttk
I was told the above is not good practice so instead I did:
import tkinter as tk
However, later on in the code I have to do something like this:
mainframe = ttk.Frame(root, padding="3 3 12 12")
This gives me an error because I didn't import ttk as its own module. I would assume something like this should work:
mainframe = tk.ttk.Frame(root, padding="3 3 12 12")
yet it also gives me an error. How do I access the ttk module if tk.ttk.Frame is not the way to do it?
ttk is not a part of the tkinter module, it is a different file in the tkinter.
In order to import ttk you have to do this
import tkinter.ttk as tkk

Can ttk safely be used with mttkinter?

I want to use mttkinter and unfortunately I must use Python 2.7.
Suprisingly, I cannot find any information on whether ttk widgets become thread safe when using mttkinter.
Do I just have to issue
from mttkinter import mtTkinter as tk
import ttk
root = tk.Tk()
# --- use tk and ttk as usual ---
or possibly alternatively
import Tkinter as tk
import ttk
import mttkinter
root = tk.Tk()
# --- use tk and ttk as usual --
and everything will work as expected? Is there a preferred version of doing the imports?
The wiki on github states
As the mtTkinter module modifies Tkinter in memory, there is no need to change anything else in your program. Just import it once somewhere in your program, and everything should work smoothly.
However, this says nothing about ttk. Can anybody give me confirmation that using ttk is fine?

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

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.

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