Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #90967 > unrolled thread

No ttk in 2.7

Started byCecil Westerhof <Cecil@decebal.nl>
First post2015-05-20 18:01 +0200
Last post2015-05-21 14:04 +0000
Articles 10 — 6 participants

Back to article view | Back to comp.lang.python


Contents

  No ttk in 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 18:01 +0200
    Re: No ttk in 2.7 Ned Batchelder <ned@nedbatchelder.com> - 2015-05-20 09:43 -0700
      Re: No ttk in 2.7 Ned Batchelder <ned@nedbatchelder.com> - 2015-05-20 09:47 -0700
        Re: No ttk in 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 20:52 +0200
          Re: No ttk in 2.7 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-05-20 21:55 +0100
      Re: No ttk in 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 20:51 +0200
    Re: No ttk in 2.7 Zachary Ware <zachary.ware+pylist@gmail.com> - 2015-05-20 12:03 -0500
      Re: No ttk in 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-05-20 20:54 +0200
        Re: No ttk in 2.7 Ian Kelly <ian.g.kelly@gmail.com> - 2015-05-20 13:30 -0600
    Re: No ttk in 2.7 Sturla Molden <sturla.molden@gmail.com> - 2015-05-21 14:04 +0000

#90967 — No ttk in 2.7

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-20 18:01 +0200
SubjectNo ttk in 2.7
Message-ID<87r3qbqlve.fsf@Equus.decebal.nl>
I want to start playing with tkinter, but there are some differences
between 2 and 3. For this I use at the moment the following code:
    import sys

    if sys.version_info[0] < 3:
        import Tkinter as tk
        import ttk
    else:
        import tkinter as tk
        from   tkinter import ttk
or can it better be done in another way?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [next] | [standalone]


#90968

FromNed Batchelder <ned@nedbatchelder.com>
Date2015-05-20 09:43 -0700
Message-ID<5e9aac07-e210-4eb5-a3d7-cfa37cb3f6ab@googlegroups.com>
In reply to#90967
On Wednesday, May 20, 2015 at 12:35:40 PM UTC-4, Cecil Westerhof wrote:
> I want to start playing with tkinter, but there are some differences
> between 2 and 3. For this I use at the moment the following code:
>     import sys
> 
>     if sys.version_info[0] < 3:
>         import Tkinter as tk
>         import ttk
>     else:
>         import tkinter as tk
>         from   tkinter import ttk
> or can it better be done in another way?

Are you sure you want this program to run under both 2.x and 3.x?
That will add a layer of complexity to your project. If your
goal is to learn how to use Tkinter, you might be better off just
picking one version of Python, and sticking with it.

Deciding what versions of Python to support is not always a simple
decision.  It should be based on a realistic assessment of who will
be using your program, and what requirements they will have.

--Ned.

[toc] | [prev] | [next] | [standalone]


#90970

FromNed Batchelder <ned@nedbatchelder.com>
Date2015-05-20 09:47 -0700
Message-ID<414bf0ad-557d-4373-8452-ab089888225e@googlegroups.com>
In reply to#90968
On Wednesday, May 20, 2015 at 12:43:29 PM UTC-4, Ned Batchelder wrote:
> On Wednesday, May 20, 2015 at 12:35:40 PM UTC-4, Cecil Westerhof wrote:
> > I want to start playing with tkinter, but there are some differences
> > between 2 and 3. For this I use at the moment the following code:
> >     import sys
> > 
> >     if sys.version_info[0] < 3:
> >         import Tkinter as tk
> >         import ttk
> >     else:
> >         import tkinter as tk
> >         from   tkinter import ttk
> > or can it better be done in another way?
> 
> Are you sure you want this program to run under both 2.x and 3.x?
> That will add a layer of complexity to your project. If your
> goal is to learn how to use Tkinter, you might be better off just
> picking one version of Python, and sticking with it.
> 
> Deciding what versions of Python to support is not always a simple
> decision.  It should be based on a realistic assessment of who will
> be using your program, and what requirements they will have.
> 
> --Ned.

Oh, and I meant to include this: if you *do* want to support both 2.x
and 3.x, the six module can make it easier.  It helps smooth over
modules that have been moved.  You can use this line, for example:

    from six.moves import tkinter_ttk as ttk

--Ned.

[toc] | [prev] | [next] | [standalone]


#90978

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-20 20:52 +0200
Message-ID<87iobnqdxi.fsf@Equus.decebal.nl>
In reply to#90970
Op Wednesday 20 May 2015 18:47 CEST schreef Ned Batchelder:

> On Wednesday, May 20, 2015 at 12:43:29 PM UTC-4, Ned Batchelder wrote:
>> On Wednesday, May 20, 2015 at 12:35:40 PM UTC-4, Cecil Westerhof wrote:
>>> I want to start playing with tkinter, but there are some
>>> differences between 2 and 3. For this I use at the moment the
>>> following code: import sys
>>>
>>> if sys.version_info[0] < 3:
>>> import Tkinter as tk
>>> import ttk
>>> else:
>>> import tkinter as tk
>>> from   tkinter import ttk
>>> or can it better be done in another way?
>>
>> Are you sure you want this program to run under both 2.x and 3.x?
>> That will add a layer of complexity to your project. If your
>> goal is to learn how to use Tkinter, you might be better off just
>> picking one version of Python, and sticking with it.
>>
>> Deciding what versions of Python to support is not always a simple
>> decision.  It should be based on a realistic assessment of who will
>> be using your program, and what requirements they will have.
>>
>> --Ned.
>
> Oh, and I meant to include this: if you *do* want to support both
> 2.x and 3.x, the six module can make it easier. It helps smooth over
> modules that have been moved. You can use this line, for example:
>
> from six.moves import tkinter_ttk as ttk

Thanks for the tip. I will look into it.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#90985

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-05-20 21:55 +0100
Message-ID<mailman.188.1432155376.17265.python-list@python.org>
In reply to#90978
On 20/05/2015 19:52, Cecil Westerhof wrote:
> Op Wednesday 20 May 2015 18:47 CEST schreef Ned Batchelder:
>
>> On Wednesday, May 20, 2015 at 12:43:29 PM UTC-4, Ned Batchelder wrote:
>>> On Wednesday, May 20, 2015 at 12:35:40 PM UTC-4, Cecil Westerhof wrote:
>>>> I want to start playing with tkinter, but there are some
>>>> differences between 2 and 3. For this I use at the moment the
>>>> following code: import sys
>>>>
>>>> if sys.version_info[0] < 3:
>>>> import Tkinter as tk
>>>> import ttk
>>>> else:
>>>> import tkinter as tk
>>>> from   tkinter import ttk
>>>> or can it better be done in another way?
>>>
>>> Are you sure you want this program to run under both 2.x and 3.x?
>>> That will add a layer of complexity to your project. If your
>>> goal is to learn how to use Tkinter, you might be better off just
>>> picking one version of Python, and sticking with it.
>>>
>>> Deciding what versions of Python to support is not always a simple
>>> decision.  It should be based on a realistic assessment of who will
>>> be using your program, and what requirements they will have.
>>>
>>> --Ned.
>>
>> Oh, and I meant to include this: if you *do* want to support both
>> 2.x and 3.x, the six module can make it easier. It helps smooth over
>> modules that have been moved. You can use this line, for example:
>>
>> from six.moves import tkinter_ttk as ttk
>
> Thanks for the tip. I will look into it.
>

https://docs.python.org/3/howto/pyporting.html

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#90977

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-20 20:51 +0200
Message-ID<87mw0zqdyy.fsf@Equus.decebal.nl>
In reply to#90968
Op Wednesday 20 May 2015 18:43 CEST schreef Ned Batchelder:

> On Wednesday, May 20, 2015 at 12:35:40 PM UTC-4, Cecil Westerhof wrote:
>> I want to start playing with tkinter, but there are some
>> differences between 2 and 3. For this I use at the moment the
>> following code: import sys
>>
>> if sys.version_info[0] < 3:
>> import Tkinter as tk
>> import ttk
>> else:
>> import tkinter as tk
>> from   tkinter import ttk
>> or can it better be done in another way?
>
> Are you sure you want this program to run under both 2.x and 3.x?
> That will add a layer of complexity to your project. If your
> goal is to learn how to use Tkinter, you might be better off just
> picking one version of Python, and sticking with it.

I just started with Tkinter and I thought this was the only
difference. But I could be wrong of-course. ;-)


> Deciding what versions of Python to support is not always a simple
> decision.  It should be based on a realistic assessment of who will
> be using your program, and what requirements they will have.

Certainly. In principle I want to work with 3, but not all libraries
work with it. (That is also why I choose Tkinter, that works with
both. wx only with 2.)

Also, in the Netherlands mostly Python 2 is used (as far as I know),
so it is good to have my code working with both from that perspective
also.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#90971

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2015-05-20 12:03 -0500
Message-ID<mailman.179.1432141452.17265.python-list@python.org>
In reply to#90967
On Wed, May 20, 2015 at 11:01 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>
> I want to start playing with tkinter, but there are some differences
> between 2 and 3. For this I use at the moment the following code:
>     import sys
>
>     if sys.version_info[0] < 3:
>         import Tkinter as tk
>         import ttk
>     else:
>         import tkinter as tk
>         from   tkinter import ttk
> or can it better be done in another way?

The way I would do it is as follows:

   try:
       import tkinter as tk
       from tkinter import ttk
   except ImportError:
       import Tkinter as tk
       import ttk

If I may suggest, just write it in Python3 first, then when it does
what you want tack on whatever you need to make 2.7 happy.  I find it
easier to do things that way, though you may find that the only thing
you have to adjust is the imports.

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#90976

FromCecil Westerhof <Cecil@decebal.nl>
Date2015-05-20 20:54 +0200
Message-ID<87egmbqdtp.fsf@Equus.decebal.nl>
In reply to#90971
Op Wednesday 20 May 2015 19:03 CEST schreef Zachary Ware:

> On Wed, May 20, 2015 at 11:01 AM, Cecil Westerhof <Cecil@decebal.nl> wrote:
>>
>> I want to start playing with tkinter, but there are some
>> differences between 2 and 3. For this I use at the moment the
>> following code: import sys
>>
>> if sys.version_info[0] < 3:
>> import Tkinter as tk
>> import ttk
>> else:
>> import tkinter as tk
>> from   tkinter import ttk
>> or can it better be done in another way?
>
> The way I would do it is as follows:
>
> try:
> import tkinter as tk
> from tkinter import ttk
> except ImportError:
> import Tkinter as tk
> import ttk

When there goes something wrong with:
    from tkinter import ttk
you will not understand what is happening. ;-)

> If I may suggest, just write it in Python3 first, then when it does
> what you want tack on whatever you need to make 2.7 happy. I find it
> easier to do things that way, though you may find that the only
> thing you have to adjust is the imports.

That was what I thought (the imports), but maybe I am wrong.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

[toc] | [prev] | [next] | [standalone]


#90979

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-05-20 13:30 -0600
Message-ID<mailman.183.1432150260.17265.python-list@python.org>
In reply to#90976
On Wed, May 20, 2015 at 12:54 PM, Cecil Westerhof <Cecil@decebal.nl> wrote:
> Op Wednesday 20 May 2015 19:03 CEST schreef Zachary Ware:
>> try:
>> import tkinter as tk
>> from tkinter import ttk
>> except ImportError:
>> import Tkinter as tk
>> import ttk
>
> When there goes something wrong with:
>     from tkinter import ttk
> you will not understand what is happening. ;-)

If something goes wrong with the first import and raises an
ImportError, then it will execute the except clause, which will
definitely raise an ImportError. In this case the second error will
simply be chained onto the first, so the details of the first error
won't be lost.

[toc] | [prev] | [next] | [standalone]


#91007

FromSturla Molden <sturla.molden@gmail.com>
Date2015-05-21 14:04 +0000
Message-ID<mailman.198.1432217081.17265.python-list@python.org>
In reply to#90967
Zachary Ware <zachary.ware+pylist@gmail.com> wrote:

> The way I would do it is as follows:
> 
>    try:
>        import tkinter as tk
>        from tkinter import ttk
>    except ImportError:
>        import Tkinter as tk
>        import ttk
> 
> If I may suggest, just write it in Python3 first, then when it does
> what you want tack on whatever you need to make 2.7 happy.  I find it
> easier to do things that way, though you may find that the only thing
> you have to adjust is the imports.

This is a good advice.

And yes, it is easier than most would think.


Sturla

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web