[Tkinter-discuss] whats Style()

Michael Lange klappnase at web.de
Sun Oct 7 17:48:32 CEST 2012

Hi Matthew,

On Sun, 7 Oct 2012 15:59:52 +0100
Matthew Ngaha <chigga101 at gmail.com> wrote:

> hi guys. in my tutorial ive come across an undefined class called Style.
> using python 3 most import statements produce an error so i mainly stick
> to:
> from tkinter import * .
> here are some of the lines of code that produces errors in different
> examples making it hard to move on:
> Style().configure("TFrame", background="#333")
> and
> self.style = Style()
> self.style.theme_use("default")
> maybe the error is in my importing but the imports the examples use
> produce errors. here are one:
> from ttk import Frame, Button, Style
> my interpreter doesnt know what ttk is.

This is again due to the changes in the organization of tkinter in
Python3. Your tutorial is probably Python2 based. 
You can see the difference in these brief python2- and -3 sessions:

$ python2.6
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from Tkinter import *
>>> import ttk
>>> style = ttk.Style()
>>> style.theme_use('clam')
>>> style.theme_use('default')
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2',
'/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old',
'/usr/lib/pymodules/python2.6', '/usr/lib/pymodules/python2.6/gtk-2.0',

$ python3.1
Python 3.1.3 (r313:86834, Nov 28 2010, 11:28:10) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from tkinter import *
>>> from tkinter import ttk
>>> style = ttk.Style()
>>> style.theme_use('default')
>>> import sys
>>> sys.path
['', '/usr/lib/python3.1', '/usr/lib/python3.1/plat-linux2',
'/usr/lib/python3/dist-packages', '/usr/lib/python3.1/dist-packages']

The reason of the different import mechanism is of course that in Python3
tkinter is not a single file "Tkinter.py" as in Python2 but now a
"package" which consists of the whole tkinter folder in your Python
install. Now when you call "from tkinter import *" everything in
tkinter/__init__.py is added to the namespace but *not* anything from any
other file in tkinter/... . If you want to use one of these you will have
to import them separately, as with "from tkinter import ttk" or
"from tkinter import messagebox" and so on.

This is because in Python3 the folder containing the tkinter
files is no longer in Python's standard module search path.
If you compare the contents of "sys.path" of Python2 and Python3 above,
you will see that the tkinter directory (here:
'/usr/lib/python3.1/tkinter' is not included, whereas in Python 2 the
tkinter directory (here: '/usr/lib/python2.6/lib-tk') is present.

This all may be confusing at the first glance, but once you get the point
I think it is quite straightforward.

I hope this helps


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Genius doesn't work on an assembly line basis.  You can't simply say,
"Today I will be brilliant."
		-- Kirk, "The Ultimate Computer", stardate 4731.3

More information about the Tkinter-discuss mailing list