Quantcast

LabelFrame question: why do options change when importing from tkFileDialog?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

LabelFrame question: why do options change when importing from tkFileDialog?

Lynn Oliver
I have a main module that includes this statement correctly:

self.fc_inFrame = LabelFrame(self.fc_Frame, height=150, width=300, text="Input File Type", labelanchor=NW, relief="groove", bd=4, font=self.boldfont)

When I move that statement to another module, I get:
_tkinter.TclError: unknown option "-bd"

If I change '-bd' to 'borderwidth' the error does not occur, but next I get:
_tkinter.TclError: unknown option "-font"

I've discovered that 'bd=4' and 'font=...' both work if I import:
from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
from Tkinter import *
import tkFont
from ttk import *

And they both fail if I import only:
from Tkinter import *
import tkFont
from ttk import *

Can anyone explain what is going on here?




_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: LabelFrame question: why do options change when importing from tkFileDialog?

Lion Kimbro-2

  I don't know for sure, but just an off-the-cuff hunch:
  The ttk widgets have different signatures than the Tk widgets.
  I think that these arguments that you are setting are (if I recall correctly) some of the arguments that are different.
  I am wondering if somehow you have an import * clobbering a value.

  Perhaps printing the value of LabelFrame to make sure that it is the class that you think that it is.


On Tue, Feb 28, 2012 at 11:22 AM, Lynn Oliver <[hidden email]> wrote:
I have a main module that includes this statement correctly:

self.fc_inFrame = LabelFrame(self.fc_Frame, height=150, width=300, text="Input File Type", labelanchor=NW, relief="groove", bd=4, font=self.boldfont)

When I move that statement to another module, I get:
_tkinter.TclError: unknown option "-bd"

If I change '-bd' to 'borderwidth' the error does not occur, but next I get:
_tkinter.TclError: unknown option "-font"

I've discovered that 'bd=4' and 'font=...' both work if I import:
from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
from Tkinter import *
import tkFont
from ttk import *

And they both fail if I import only:
from Tkinter import *
import tkFont
from ttk import *

Can anyone explain what is going on here?




_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss



_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: LabelFrame question: why do options change when importing from tkFileDialog?

Bryan Oakley-2
In reply to this post by Lynn Oliver
On Tue, Feb 28, 2012 at 1:22 PM, Lynn Oliver <[hidden email]> wrote:

> I've discovered that 'bd=4' and 'font=...' both work if I import:
> from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
> from Tkinter import *
> import tkFont
> from ttk import *
>
> And they both fail if I import only:
> from Tkinter import *
> import tkFont
> from ttk import *
>
> Can anyone explain what is going on here?

Both Tkinter and ttk define objects with the same name, such as Button
and Label. These two widgets don't share the same set of options. When
you do "import *", whichever one you import second "wins". Thus, in
one file you might be getting a tk button with one set of options, and
in another you get a ttk button with a different set, even though in
both files you use "Button".

IMHO this is a perfect example why you should _never_ "import *".
Instead, I wholeheartedly recommend always working like this:

    import Tkinter as tk
    import ttk
    ...
    tk.Button(...)
    ttk.Button(...)

With that, it becomes completely obvious which sort of button or label
you are creating. Even if you don't mix tk and ttk widgets I think you
should import this way. It makes the code more self-documenting at the
expense of a tiny bit more typing.
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Fwd: LabelFrame question: UPDATE

Lynn Oliver
In reply to this post by Lynn Oliver
My apologies, but on further investigation I discovered that the problem is solved if I put  "Tkinter import " after "from ttk import " it works. 

The order was wrong in the main routine, but it seems that importing another module that imports Tkinter will fix the conflict.



Begin forwarded message:

From: Lynn Oliver <[hidden email]>
Subject: LabelFrame question: why do options change when importing from tkFileDialog?
Date: February 28, 2012 11:22:14 AM PST
To: Python-Tkinter <[hidden email]>

I have a main module that includes this statement correctly:

self.fc_inFrame = LabelFrame(self.fc_Frame, height=150, width=300, text="Input File Type", labelanchor=NW, relief="groove", bd=4, font=self.boldfont)

When I move that statement to another module, I get:
_tkinter.TclError: unknown option "-bd"

If I change '-bd' to 'borderwidth' the error does not occur, but next I get:
_tkinter.TclError: unknown option "-font"

I've discovered that 'bd=4' and 'font=...' both work if I import:
from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
from Tkinter import *
import tkFont
from ttk import *

And they both fail if I import only:
from Tkinter import *
import tkFont
from ttk import *

Can anyone explain what is going on here?





_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: LabelFrame question: why do options change when importing from tkFileDialog?

Lynn Oliver
In reply to this post by Bryan Oakley-2
While I was chasing this down Bryan provided the correct answer.  Thanks, Bryan.  I believe I will follow your advice.

On Feb 28, 2012, at 12:47 PM, Bryan Oakley wrote:

> On Tue, Feb 28, 2012 at 1:22 PM, Lynn Oliver <[hidden email]> wrote:
>> I've discovered that 'bd=4' and 'font=...' both work if I import:
>> from tkFileDialog import askopenfilename, asksaveasfilename, askdirectory
>> from Tkinter import *
>> import tkFont
>> from ttk import *
>>
>> And they both fail if I import only:
>> from Tkinter import *
>> import tkFont
>> from ttk import *
>>
>> Can anyone explain what is going on here?
>
> Both Tkinter and ttk define objects with the same name, such as Button
> and Label. These two widgets don't share the same set of options. When
> you do "import *", whichever one you import second "wins". Thus, in
> one file you might be getting a tk button with one set of options, and
> in another you get a ttk button with a different set, even though in
> both files you use "Button".
>
> IMHO this is a perfect example why you should _never_ "import *".
> Instead, I wholeheartedly recommend always working like this:
>
>    import Tkinter as tk
>    import ttk
>    ...
>    tk.Button(...)
>    ttk.Button(...)
>
> With that, it becomes completely obvious which sort of button or label
> you are creating. Even if you don't mix tk and ttk widgets I think you
> should import this way. It makes the code more self-documenting at the
> expense of a tiny bit more typing.

_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Fwd: LabelFrame question: UPDATE

Michael Lange
In reply to this post by Lynn Oliver
Hi,

Thus spoketh Lynn Oliver <[hidden email]>
unto us on Tue, 28 Feb 2012 12:51:36 -0800:

> My apologies, but on further investigation I discovered that the
> problem is solved if I put  "Tkinter import " after "from ttk import "
> it works.
>
> The order was wrong in the main routine, but it seems that importing
> another module that imports Tkinter will fix the conflict.
>
>

I have not read the whole thread, so I am not sure if this actually was
the root of your problem, but if you do:

from Tkinter import *
from ttk import *

of course the LabelFrame class imported from ttk will override the one
that came from Tkinter! You can easily verify this from a shell:

$ python
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 *
>>> LabelFrame
<class Tkinter.LabelFrame at 0xf708bbfc>
>>> from ttk import *
>>> LabelFrame
<class ttk.Labelframe at 0xf62eb89c>
>>>


So while it is generally considered to be save to
do "from Tkinter import *" I would strongly recommend to prefer "import
ttk" to "from ttk import *" in order to avoid namespace confusion.

Regards

Michael


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

Men of peace usually are [brave].
                -- Spock, "The Savage Curtain", stardate 5906.5
_______________________________________________
Tkinter-discuss mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/tkinter-discuss
Loading...