Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [newbie] struggling wth tkinter

Reply
Thread Tools

[newbie] struggling wth tkinter

 
 
Jean Dubois
Guest
Posts: n/a
 
      12-07-2013
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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      12-07-2013
On Sat, 7 Dec 2013 08:52:08 -0800 (PST), Jean Dubois
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Cousin Stanley
Guest
Posts: n/a
 
      12-07-2013

> ....
> 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
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      12-07-2013
On 12/7/2013 1:12 PM, Dave Angel wrote:
> On Sat, 7 Dec 2013 08:52:08 -0800 (PST), Jean Dubois
> <(E-Mail Removed)> 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

 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      12-08-2013
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
>
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      12-08-2013
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
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      12-08-2013
On Sun, Dec 8, 2013 at 6:40 PM, Jean Dubois <(E-Mail Removed)> 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
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      12-08-2013
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
 
Reply With Quote
 
Jean Dubois
Guest
Posts: n/a
 
      12-08-2013
Op zondag 8 december 2013 08:49:22 UTC+1 schreef Chris Angelico:
> On Sun, Dec 8, 2013 at 6:40 PM, Jean Dubois <(E-Mail Removed)> 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
 
Reply With Quote
 
Dave Angel
Guest
Posts: n/a
 
      12-08-2013
On Sat, 7 Dec 2013 23:45:06 -0800 (PST), Jean Dubois
<(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: Struggling again 'map' mosscliffe Python 6 05-27-2007 12:32 AM
Newbie struggling withwireless =?Utf-8?B?am9iYmVycw==?= Wireless Networking 3 07-13-2006 04:50 PM
Newbie struggling with function pointers larkmore@aol.com C Programming 8 05-17-2005 12:35 PM
Java Newbie struggling with threads Bob Dubery Java 10 06-11-2004 02:26 PM
Struggling wth new installation Lee Davison Computer Support 3 02-01-2004 11:11 PM



Advertisments