[tkinter] ImportError: cannot import name 'ttk'?

Post here if you need help with creating a Graphical User Interface in Python.

[tkinter] ImportError: cannot import name 'ttk'?

Postby candewish » Thu May 01, 2014 5:14 pm

Many tkinter python scripts that execute in verion 2.7 or when run from IDLE under
fersion 3.4 will fail if executed from a Windows DOS command.

tk_hello.py
Code: Select all
#! /usr/bin/python
from tkinter import *
import tkinter.ttk as ttk
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

Windows XP, Python 3.4
Code: Select all
C:\Documents and Settings\user>python tk_hello.py

(Script executes successfully)

(attempting to execute this script in a sub-directory fails)
Code: Select all
C:\Documents and Settings\user>python "c:\Documents and Settings\user\My Doc
uments\py3_apps\tk_hello.py"


Code: Select all
Traceback (most recent call last):
  File "c:\Documents and Settings\user\My documents\py3_apps\tk_hello.py", lin
e 4, in <module>
    from tkinter import *
  File "c:\Documents and Settings\user\My documents\py3_apps\tkinter.py", line
 2, in <module>
    from tkinter import ttk
ImportError: cannot import name 'ttk'


(switching to the directory first produces the same error)
Code: Select all
C:\Documents and Settings\user>cd "My documents"\py3_apps


Code: Select all
C:\Documents and Settings\user\My Documents\py3_apps>python tk_hello.py
Traceback (most recent call last):
  File "tk_hello.py", line 4, in <module>
    from tkinter import *
  File "C:\Documents and Settings\user\My Documents\py3_apps\tkinter.py", line
 2, in <module>
    from tkinter import ttk
ImportError: cannot import name 'ttk'


Traceback suggests that executing python from, or referring python to, another
directory causes it to search for ttk in a tkinter.py module in that directory.
Fix?? (Note, I have experimented with all manner of tkinter import commands
without success.)
Last edited by Yoriz on Thu May 01, 2014 7:40 pm, edited 2 times in total.
Reason: First post lock, Added code tags, Changed Title.
candewish
 
Posts: 2
Joined: Thu May 01, 2014 3:24 pm

Re: Python3 tkinter script executed from command line

Postby Yoriz » Thu May 01, 2014 5:26 pm

Hi welcome to the forum, please read the new users read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 784
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Python3 tkinter script executed from command line

Postby stranac » Thu May 01, 2014 5:51 pm

If you have a script named tkinter.py in a directory, and run python from that directory, import tkinter will import that script, not what you want.
Rename that script, remove the matching tkinter.pyc (if it exists), and it should work fine.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1127
Joined: Thu Feb 07, 2013 3:42 pm

Re: Python3 tkinter script executed from command line

Postby candewish » Thu May 01, 2014 7:36 pm

You're Right!!!!! I have no recollection of ever creating the tkinter.py file, but there is was.
Thank you for pointing out what should have been obvious to me!
candewish
 
Posts: 2
Joined: Thu May 01, 2014 3:24 pm

Re: [tkinter] ImportError: cannot import name 'ttk'?

Postby metulburr » Thu May 01, 2014 7:44 pm

ive done that before. It's something you only ever do once.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1422
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY


Return to GUI

Who is online

Users browsing this forum: No registered users and 0 guests