[newbie] struggling wth tkinter

Discussion in 'Python' started by Jean Dubois, Dec 7, 2013.

  1. Jean Dubois

    Jean Dubois Guest

    I'm trying to go through a tutorial on tkinter which has the code below as an example. The only thing I see when running it is a little popup with "Click mouse here to quit" which works as expected but always shows the following error-message.
    However the "main" window which should let you enter the numbers is not shown.

    This is the quit error message:
    Traceback (most recent call last):
    File "./feet2meters.py", line 3, in <module>
    from tkinter import ttk
    ImportError: cannot import name ttk

    This is the code:
    #!/usr/bin/env python
    from tkinter import *
    from tkinter import ttk

    def calculate(*args):
    try:
    value = float(feet.get())
    meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
    except ValueError:
    pass

    root = Tk()
    root.title("Feet to Meters")

    mainframe = ttk.Frame(root, padding="3 3 12 12")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)

    feet = StringVar()
    meters = StringVar()

    feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
    feet_entry.grid(column=2, row=1, sticky=(W, E))

    ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
    ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

    ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
    ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
    ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

    for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

    feet_entry.focus()
    root.bind('<Return>', calculate)

    root.mainloop()

    thanks in advance
    jean
    Jean Dubois, Dec 7, 2013
    #1
    1. Advertising

  2. Jean Dubois

    Dave Angel Guest

    On Sat, 7 Dec 2013 08:52:08 -0800 (PST), Jean Dubois
    <> wrote:
    > I'm trying to go through a tutorial on tkinter which has the code

    below as an example. The only thing I see when running it is a little
    popup with "Click mouse here to quit" which works as expected but
    always shows the following error-message.
    > However the "main" window which should let you enter the numbers is

    not shown.
    > This is the quit error message:
    > Traceback (most recent call last):
    > File "./feet2meters.py", line 3, in <module>
    > from tkinter import ttk
    > ImportError: cannot import name ttk


    > This is the code:
    > #!/usr/bin/env python
    > from tkinter import *
    > from tkinter import ttk


    Thanks for supplying the complete traceback. But you should also
    tell the python version and what OS. I'll guess python 3.3 on Linux.


    Finally, what version tk are you running? These widgets were
    introduced in tk 8.5

    Since it failed on the second import, none of the rest of the code
    matters. However, since you're not running it from a terminal window,
    it's conceivable that your ide is affecting the result.

    --
    DaveA
    Dave Angel, Dec 7, 2013
    #2
    1. Advertising


  3. > ....
    > The only thing I see when running it is a little popup
    > with "Click mouse here to quit" which works as expected
    > but always shows the following error-message.


    This seems to be left over from an earlier post
    where you were binding a mouse event to a tk label

    Did you create a new file ?


    > However the "main" window which should let you enter the
    > numbers is not shown.
    >
    > This is the quit error message:
    > Traceback (most recent call last):
    > File "./feet2meters.py", line 3, in <module>
    > from tkinter import ttk
    > ImportError: cannot import name ttk
    >
    > This is the code:
    > ....


    If I copy/paste your code as posted
    into a new file named ftom.py
    and change the she-bang line
    as follows ....

    #!/usr/bin/env python3

    Then from the command line ....

    python3 ftom.py

    Your code runs as expected
    using python 3.2.3 ....


    --
    Stanley C. Kitching
    Human Being
    Phoenix, Arizona
    Cousin Stanley, Dec 7, 2013
    #3
  4. Jean Dubois

    Terry Reedy Guest

    On 12/7/2013 1:12 PM, Dave Angel wrote:
    > On Sat, 7 Dec 2013 08:52:08 -0800 (PST), Jean Dubois
    > <> wrote:
    >> I'm trying to go through a tutorial on tkinter which has the code

    > below as an example. The only thing I see when running it is a little
    > popup with "Click mouse here to quit" which works as expected but always
    > shows the following error-message.
    >> However the "main" window which should let you enter the numbers is

    > not shown.
    >> This is the quit error message:
    >> Traceback (most recent call last):
    >> File "./feet2meters.py", line 3, in <module>
    >> from tkinter import ttk
    >> ImportError: cannot import name ttk


    That is supposed to work.

    >> This is the code:
    >> #!/usr/bin/env python
    >> from tkinter import *
    >> from tkinter import ttk

    >
    > Thanks for supplying the complete traceback. But you should also tell
    > the python version and what OS. I'll guess python 3.3 on Linux.
    >
    > Finally, what version tk are you running? These widgets were
    > introduced in tk 8.5


    In 8.4, they were in the Tile extension. I do not know if that will
    work. Better, probably, to upgrade.
    >
    > Since it failed on the second import, none of the rest of the code
    > matters. However, since you're not running it from a terminal window,
    > it's conceivable that your ide is affecting the result.


    Until you are able to import ttk, I believe you could remove the import
    and all 'ttk.' appearances, as I do not see anything ttk-specific.


    --
    Terry Jan Reedy
    Terry Reedy, Dec 7, 2013
    #4
  5. Jean Dubois

    Jean Dubois Guest

    Op zaterdag 7 december 2013 19:12:50 UTC+1 schreef Dave Angel:
    > On Sat, 7 Dec 2013 08:52:08 -0800 (PST), Jean Dubois
    >
    > <> wrote:
    >
    > > I'm trying to go through a tutorial on tkinter which has the code

    >
    > below as an example. The only thing I see when running it is a little
    >
    > popup with "Click mouse here to quit" which works as expected but
    >
    > always shows the following error-message.
    >
    > > However the "main" window which should let you enter the numbers is

    >
    > not shown.
    >
    > > This is the quit error message:

    >
    > > Traceback (most recent call last):

    >
    > > File "./feet2meters.py", line 3, in <module>

    >
    > > from tkinter import ttk

    >
    > > ImportError: cannot import name ttk

    >
    >
    >
    > > This is the code:

    >
    > > #!/usr/bin/env python

    >
    > > from tkinter import *

    >
    > > from tkinter import ttk

    >
    >
    >
    > Thanks for supplying the complete traceback. But you should also
    >
    > tell the python version and what OS. I'll guess python 3.3 on Linux.
    >
    >
    >
    >
    >
    > Finally, what version tk are you running? These widgets were
    >
    > introduced in tk 8.5
    >
    >
    >
    > Since it failed on the second import, none of the rest of the code
    >
    > matters. However, since you're not running it from a terminal window,
    >
    > it's conceivable that your ide is affecting the result.
    >
    >
    >
    > --
    >
    > DaveA


    I have two pythons installed on my system:
    Python 2.7.3 and Python 3.2.3
    When using python2 I get the errors mentioned above
    When using python3 (I removed the shebang and started as python3 feettometers.py) then I get these errors:

    coolens@antec2:~$ python3 feet2meters.py
    Traceback (most recent call last):
    File "feet2meters.py", line 1, in <module>
    from tkinter import *
    File "/home/coolens/tkinter.py", line 2, in <module>
    import Tkinter as tk
    ImportError: No module named Tkinter

    I tried to fix this by installing
    apt-get install python3-tk (python3-tk_3.2.3-1_amd64.deb)

    but the error remains

    What should I do now?

    thanks in advance
    jean
    Jean Dubois, Dec 8, 2013
    #5
  6. Jean Dubois

    Jean Dubois Guest

    Op zaterdag 7 december 2013 19:23:30 UTC+1 schreef Cousin Stanley:
    > > ....

    >
    > > The only thing I see when running it is a little popup

    >
    > > with "Click mouse here to quit" which works as expected

    >
    > > but always shows the following error-message.

    >
    >
    >
    > This seems to be left over from an earlier post
    >
    > where you were binding a mouse event to a tk label
    >
    >
    >
    > Did you create a new file ?
    >
    >
    >
    >
    >
    > > However the "main" window which should let you enter the

    >
    > > numbers is not shown.

    >
    > >

    >
    > > This is the quit error message:

    >
    > > Traceback (most recent call last):

    >
    > > File "./feet2meters.py", line 3, in <module>

    >
    > > from tkinter import ttk

    >
    > > ImportError: cannot import name ttk

    >
    > >

    >
    > > This is the code:

    >
    > > ....

    >
    >
    >
    > If I copy/paste your code as posted
    >
    > into a new file named ftom.py
    >
    > and change the she-bang line
    >
    > as follows ....
    >
    >
    >
    > #!/usr/bin/env python3
    >
    >
    >
    > Then from the command line ....
    >
    >
    >
    > python3 ftom.py
    >
    >
    >
    > Your code runs as expected
    >
    > using python 3.2.3 ....

    I tried you suggestion above:

    This is what I get:
    Traceback (most recent call last):
    File "./feet2meters.py", line 2, in <module>
    from tkinter import *
    File "/home/jean/tkinter.py", line 2, in <module>
    import Tkinter as tk
    ImportError: No module named Tkinter

    and this is my python3-version:
    Python 3.2.3 (default, Sep 25 2013, 18:22:43)
    [GCC 4.6.3] on linux2

    any idea what is going wrong?

    thanks in advance
    jean
    Jean Dubois, Dec 8, 2013
    #6
  7. On Sun, Dec 8, 2013 at 6:40 PM, Jean Dubois <> wrote:
    > coolens@antec2:~$ python3 feet2meters.py
    > ImportError: No module named Tkinter


    In Python 3, the module's named tkinter instead of Tkinter. You should
    be able to do the exact same import but with the lower-case name, or
    if you need to support both:

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

    ChrisA
    Chris Angelico, Dec 8, 2013
    #7
  8. Jean Dubois

    Jean Dubois Guest

    Op zondag 8 december 2013 09:10:28 UTC+1 schreef Christian Gollwitzer:
    > Am 07.12.13 17:52, schrieb Jean Dubois:
    >
    > > I'm trying to go through a tutorial on tkinter which has the code below as an example. The only thing I see when running it is a little popup with "Click mouse here to quit" which works as expected but always shows the following error-message.

    >
    > > However the "main" window which should let you enter the numbers is not shown.

    >
    > >

    >
    > > This is the quit error message:

    >
    > > Traceback (most recent call last):

    >
    > > File "./feet2meters.py", line 3, in <module>

    >
    > > from tkinter import ttk

    >
    > > ImportError: cannot import name ttk

    >
    > >

    >
    > > This is the code:

    >
    > > #!/usr/bin/env python

    >
    >
    >
    > > from tkinter import *

    >
    > > from tkinter import ttk

    >
    >
    >
    > With my python2, it works to replace the import by
    >
    >
    >
    > from Tkinter import *
    >
    > import ttk
    >
    >
    >
    > Christian


    Thank you very much Christian, this solves the problem

    kind regards,
    jean
    Jean Dubois, Dec 8, 2013
    #8
  9. Jean Dubois

    Jean Dubois Guest

    Op zondag 8 december 2013 08:49:22 UTC+1 schreef Chris Angelico:
    > On Sun, Dec 8, 2013 at 6:40 PM, Jean Dubois <> wrote:
    >
    > > coolens@antec2:~$ python3 feet2meters.py

    >
    > > ImportError: No module named Tkinter

    >
    >
    >
    > In Python 3, the module's named tkinter instead of Tkinter. You should
    >
    > be able to do the exact same import but with the lower-case name, or
    >
    > if you need to support both:
    >
    >
    >
    > try:
    >
    > import tkinter as tk
    >
    > except ImportError:
    >
    > import Tkinter as tk
    >

    This seems a very nice approach, but can you tell me how I should change the import-section as a whole; I tried it like here below (but that's certainly not the right way):


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

    thanks in advance
    jean
    Jean Dubois, Dec 8, 2013
    #9
  10. Jean Dubois

    Dave Angel Guest

    On Sat, 7 Dec 2013 23:45:06 -0800 (PST), Jean Dubois
    <> wrote:
    > This is what I get:
    > Traceback (most recent call last):
    > File "./feet2meters.py", line 2, in <module>
    > from tkinter import *
    > File "/home/jean/tkinter.py", line 2, in <module>
    > import Tkinter as tk
    > ImportError: No module named Tkinter


    Regardless of your other fixes, you should rename the bogus file:

    /home/jean/tkinter.py

    You very seldom want to have files that can shadow system modules.

    --
    DaveA
    Dave Angel, Dec 8, 2013
    #10

  11. > ....
    > This is what I get:
    >
    > Traceback (most recent call last):
    > File "./feet2meters.py", line 2, in <module>
    > from tkinter import *
    > File "/home/jean/tkinter.py", line 2, in <module>
    > import Tkinter as tk
    > ImportError: No module named Tkinter
    > ....


    From your original post I only changed the she-bang line
    from python to python3 ....

    #!/usr/bin/env python3

    and the import lines remained
    as you originally posted
    which are appropriate for python3 ....

    from tkinter import *
    from tkinter import ttk

    The traceback above shows an import statement
    that is different from the one you originally posted
    which would be ok in python2 but not python3 ....

    import Tkinter as tk


    --
    Stanley C. Kitching
    Human Being
    Phoenix, Arizona
    Cousin Stanley, Dec 8, 2013
    #11
  12. Jean Dubois

    Jean Dubois Guest

    Op zondag 8 december 2013 15:16:25 UTC+1 schreef Dave Angel:
    > On Sat, 7 Dec 2013 23:45:06 -0800 (PST), Jean Dubois
    >
    > <> wrote:
    >
    > > This is what I get:

    >
    > > Traceback (most recent call last):

    >
    > > File "./feet2meters.py", line 2, in <module>

    >
    > > from tkinter import *

    >
    > > File "/home/jean/tkinter.py", line 2, in <module>

    >
    > > import Tkinter as tk

    >
    > > ImportError: No module named Tkinter

    >
    >
    >
    > Regardless of your other fixes, you should rename the bogus file:
    >
    >
    >
    > /home/jean/tkinter.py
    >
    >
    >
    > You very seldom want to have files that can shadow system modules.
    >
    >
    >
    > --
    >
    > DaveA


    Thank you Dave, this made it work under python3 too. I also had to remove tkinter.pyc

    kind regards,
    jean
    Jean Dubois, Dec 8, 2013
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wayne Myers

    POK Flag Weirdness wth binary files

    Wayne Myers, May 26, 2004, in forum: Perl
    Replies:
    1
    Views:
    472
    ! aaa
    May 27, 2004
  2. Guest
    Replies:
    1
    Views:
    549
    Matt Berther
    Apr 20, 2005
  3. Ranpha
    Replies:
    3
    Views:
    585
    Ranpha
    Apr 4, 2005
  4. Ben

    throw wth no operand

    Ben, Jan 9, 2004, in forum: C++
    Replies:
    2
    Views:
    333
  5. Michael Glassford

    Problems wth os.stat().st_mtime on Mac

    Michael Glassford, Sep 29, 2006, in forum: Python
    Replies:
    2
    Views:
    639
    Michael Glassford
    Oct 2, 2006
Loading...

Share This Page