Weird ttk behaviour

Rotwang sg552 at hotmail.co.uk
Mon Sep 16 12:28:24 EDT 2013


Hi all,

I've just started trying to learn how to use ttk, and I've discovered 
something that I don't understand. I'm using Python 3.3.0 in Linux Mint 
15. Suppose I create the following module:

# begin tkderp.py

import tkinter as tk
import tkinter.messagebox as _
from tkinter import ttk
from imp import reload


_root = tk.Tk()
_root.withdraw()

def f():
	style = ttk.Style(_root)
	style.theme_create('newtheme', parent = 'default')
	tk.messagebox.showwarning('test', 'test')
	style.theme_use('newtheme')
	tk.messagebox.showwarning('test', 'test')

# end tkderp.py


The function f() is supposed to spawn two warning dialogs. AIUI, the 
"style.theme.use('newtheme')" line shouldn't make any difference - it 
would refresh any existing ttk widgets and alter the appearance of any 
subsequently created ones if I had changed any of the new theme's 
settings, but in the above context nothing it does should be discernible 
to the user. If I try to call f() the first warning gets shown, but the 
second one causes an exception:

Python 3.3.1 (default, Apr 17 2013, 22:30:32)
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> import tkderp
 >>> tkderp.f()
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/phil/Python/tkderp.py", line 17, in f
     tk.messagebox.showwarning('test', 'test')
   File "/usr/lib/python3.3/tkinter/messagebox.py", line 87, in showwarning
     return _show(title, message, WARNING, OK, **options)
   File "/usr/lib/python3.3/tkinter/messagebox.py", line 72, in _show
     res = Message(**options).show()
   File "/usr/lib/python3.3/tkinter/commondialog.py", line 48, in show
     s = w.tk.call(self.command, *w._options(self.options))
_tkinter.TclError: unknown color name ""


If I try reloading the module and calling the function again, this time 
the first of the two warnings raises the exception. Since I don't really 
understand how the ttk.Style class works, I can't say whether this 
behaviour is expected or not. But here's what's weird. Suppose that I 
comment out the last two lines in the definition of f(), like so:

# begin modified tkderp.py

import tkinter as tk
import tkinter.messagebox as _
from tkinter import ttk
from imp import reload


_root = tk.Tk()
_root.withdraw()

def f():
	style = ttk.Style(_root)
	style.theme_create('newtheme', parent = 'default')
	tk.messagebox.showwarning('test', 'test')
	#style.theme_use('newtheme')
	#tk.messagebox.showwarning('test', 'test')

# end modified tkderp.py


Unsurprisingly, importing the module and calling f() displays a single 
warning dialog and raises no exception. If I then uncomment those two 
lines, reload the module and call f() again (by entering 
tkderp.reload(tkderp).f()), the function works like it was supposed to 
in the first place: two warnings, no exceptions. I can reload the module 
as many times as I like and f() will continue to work without any problems.

On Windows 7 (sys.version is '3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 
10:57:17) [MSC v.1600 64 bit (AMD64)]') there's no problem; f() works 
fine in the first place. Does anybody know what's going on?



More information about the Python-list mailing list