Weird ttk behaviour

Rotwang sg552 at hotmail.co.uk
Tue Sep 17 10:25:43 EDT 2013


On 17/09/2013 12:32, Chris Angelico wrote:
> [...]
>
> If reloading and doing it again makes things different, what happens
> if you simply trigger your code twice without reloading?
>
> I've no idea if it'll help, it just seems like an attack vector on the
> problem, so to speak.

Thanks for the suggestion, here's what I've found with some more 
testing. If I rewrite the function f() like this:

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


then I import the module and call f(False) followed by f(True), the 
second call raises an exception just like the original function. I've 
tried variations of the above, such as defining a module-level global 
style instead of having one created during the function call, and the 
end result is always the same. However, suppose instead I define two 
modules, tkderp and tkderp2; both have a function f as defined in my OP, 
but the first has the last two lines of f commented out, and the second 
doesn't (i.e. tkderp is the modified tkderp from before, and tkderp2 is 
the original). Then I do this:

 >>> import tkderp
 >>> tkderp.f()
 >>> import tkderp2
 >>> tkderp2.f()


In that case the second call to f() works fine - two warnings, no 
exception. In fact, if I replace tkderp with this:

# begin tkderp.py

import tkinter as tk

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

# end tkderp.py


then simply importing tkderp before tkderp2 is enough to make the latter 
work properly - this

 >>> import tkderp2
 >>> tkderp2.f()


raises an exception, but this

 >>> import tkderp
 >>> import tkderp2
 >>> tkderp2.f()


doesn't. Any ideas what may be going on?



More information about the Python-list mailing list