Can ttk safely be used with mttkinter? - python

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?

Related

root = tkinter.Tk() or root = Tk()?

I have two scripts that both work:
import tkinter
root = tkinter.Tk()
root.configure(bg='blue')
root.mainloop()
and
from tkinter import *
root = Tk()
text = Text(root)
text.insert(INSERT, "Hello world!")
text.pack()
root.mainloop()
I want to combine the two scripts to print the text on a blue background, but moving anything from one script to another seems to break it.
I can't figure out if it's about root = tkinter.Tk() vs root = Tk(), or import tkinter vs from tkinter import *, or something entirely different. I can't find a successful combination.
I'm using Ubuntu and Python 3.6.9.
Because you use two different styles when importing tkinter, you will need to modify the code from one file when moving to the other. The code in your first example is the preferred way to do it because PEP8 discourages wildcard imports.
When when you copy the code from the second example, you'll need to add tkinter. to every tkinter command (tkinter.Tk(), tkinter.Text(root), tk.INSERT, etc.
Personally I find import tkinter as tk to be a slight improvement. I find tk.Tk() to be a little easier to type and read than tkinter.Tk().
You should know that:
from tkinter import *
will import all the attribute in the tkinter.But if you also define some variable in your script.It will be covered by your new variable.So we don't recommend you to use that.(If you used both from tkinter.ttk import * and from tkinter import *.Some default widgets of tkinter will be covered by ttk widgets.)
Just like Mr.Bryan said,I'd like to use import tkinter as tk,too.

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

ubuntu filedialog.askdirectory too low version

I was trying to call the file dialog of ubuntu to choose a directory with python3.6, and the code looks like this:
from tkinter import filedialog
filedialog.askdirectory()
but when i run this, a very old version file dialog shows:
Any idea on how to call the newest file dialog of ubuntu using python?
It is not old version, it is standard theme for GTK. You would have to use theme to change it. But Linux has only three styles as default
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
root = tk.Tk()
root.style = ttk.Style()
print(root.style.theme_names())
root.style.theme_use('clam')
filedialog.askdirectory()
root.mainloop()
classis/default:
clam:
alt:
You can get more themes installing module
pip install ttkthemes
And code
import tkinter as tk
from tkinter import ttk
import ttkthemes
root = tk.Tk()
root.style = ttkthemes.ThemedStyle()
for i, name in enumerate(sorted(root.style.theme_names())):
b = ttk.Button(root, text=name, command=lambda name=name:root.style.theme_use(name))
b.pack(fill='x')
root.mainloop()
List of styles
kroc:
radiance:
The UI components provided by tkinter (and the underlying tk library) are different from the UI components provided by, say, the GTK or the Qt libraries that are probably used by your desktop.
tkinter has a set of alternative widgets, that you can access with
from tkinter.ttk import *
that support the look and feel of your desktop, but (afaict) unfortunately the
filedialog widget is not supported.

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