Jani
Jani

Reputation: 117

How to fix 'ImportError: cannot import name 'ttk'' in Python 3.6.8

I am trying to create a GUI for a Python program I wrote. For that I'd like to import ttk from the tkinter module, which (as far as I know) shouldn't require further installations in Python 3.6.8. I have been programming for 3 months now, so please excuse if the answer is trivial for you, I am still new to Python (or programming in general ^^).

I have already tried following versions to import ttk:

import tkinter as tk
from tkinter import ttk 

import tkinter as tk
from tkinter.tkk import ttk 

import tkinter as tk
from tkinter.ttk import *

The last two gave me the error 'No module named 'tkinter.ttk'; 'tkinter' is not a package'

from tkinter import *
import ttk

So how do I import ttk in Python 3.6.8 without running an error? Thanks for your help.

Upvotes: 1

Views: 9874

Answers (5)

Ciprian Ionel Radu
Ciprian Ionel Radu

Reputation: 21

I faced a similar matter (maybe this can help you):

I received the error:

ImportError: cannot import name 'ttk' from partially initialized module 'tkinter'

Because in my example I tried to create a test file with the name tkinter.py.

I renamed this test file and the error disappeared.

Good luck.

Upvotes: 0

oscar
oscar

Reputation: 1

The correct syntax for this is

from tkinter import *
from tkinter import ttk

Upvotes: 0

NotTheDr01ds
NotTheDr01ds

Reputation: 20627

Answered in the comments. Moving to an answer since this question continues to occasionally get new answers that miss the comment.

According to this comment from the user who asked the question:

I fixed it by reinstalling and deleting the pycache. Then the first one worked for importing

The "first one" being:

import tkinter as tk
from tkinter import ttk 

... from the original question.

Upvotes: 0

Elena
Elena

Reputation: 321

I was having the same issue and this worked for me:

"brew install [email protected]"

Upvotes: 0

Vivek Khimani
Vivek Khimani

Reputation: 75

You cannot use tkinter unless it is installed on your machine with Python 3.6.8. To check if tkinter is installed, go to the terminal (Command Prompt for Windows) and type the following command:

python -m tkinter

If this command opens a window displaying a simple Tk interface, that means it's installed correctly and you should be able to import the tkinter package in your program.

However, if it shows an error, I recommend reinstalling Python and make sure that you select TCL/TK and IDLE when it asks you for optional installations.

Let me know if that works.

Upvotes: 1

Related Questions