[newbie] struggling wth tkinter


J

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

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
 
Ad

Advertisements

D

Dave Angel

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.
 
C

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 ....
 
T

Terry Reedy

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.

That is supposed to work.
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.
 
J

Jean Dubois

Op zaterdag 7 december 2013 19:12:50 UTC+1 schreef Dave Angel:
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.


not shown.











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.

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:

[email protected]:~$ 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
 
J

Jean Dubois

Op zaterdag 7 december 2013 19:23:30 UTC+1 schreef Cousin Stanley:
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 ?














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
 
Ad

Advertisements

C

Chris Angelico

[email protected]:~$ 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
 
J

Jean Dubois

Op zondag 8 december 2013 09:10:28 UTC+1 schreef Christian Gollwitzer:
Am 07.12.13 17:52, schrieb Jean Dubois:











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
 
J

Jean Dubois

Op zondag 8 december 2013 08:49:22 UTC+1 schreef Chris Angelico:
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
 
D

Dave Angel

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.
 
C

Cousin Stanley

....
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
 
Ad

Advertisements

J

Jean Dubois

Op zondag 8 december 2013 15:16:25 UTC+1 schreef Dave Angel:
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.

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

kind regards,
jean
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top