4

Trying to use Treeview in a project but just got hit with an error; module 'tkinter' has no attribute 'Treeview'

Here's my code;

import tkinter as tk
from tkinter import *
import tkinter as ttk

class MainGUI:
    def __init__(self, master):
        self.master = master

        self.EmpInfo = ttk.Treeview(self.master).grid(row = 1 , column = 1)


def main():
    root = tk.Tk()
    a = MainGUI(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Do i need to pip install more stuff or am i just using Treeview wrong?

3
  • from tkinter import ttk, and then self.EmpInfo = ttk.Treeview(self.master) Commented Sep 30, 2018 at 17:37
  • @HenryYik Thanks for the reply, i tired that and it did not work. Commented Sep 30, 2018 at 17:47
  • 4
    import tkinter as ttk isn't doing what you think. It's not importing ttk, it's just importing tkinter and giving it a different name. That won't cause it to have the Treeview class. Commented Oct 1, 2018 at 14:43

2 Answers 2

7

You are using Treeview wrong. It's in the ttk module. You need to import ttk, and then use Treeview from the ttk module

from tkinter import ttk
...
self.EmpInfo = ttk.Treeview(...)
...
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the reply, i tried import tkinter as ttk which still gave me the same error.
@hiihihihelloo: you may have other problems, but I can say with absolute certainty that this is part of the problem. It is simply a fact that tk does not have a Treeview widget.
I tried to uninstall and reinstall python but got the same result, could it possibly be a tkinter problem? Is there anyway i can update tkinter?
@hiihihihelloo: no, the problem is exactly what my answer says and what the error message is telling you. Tkinter does not have a treeview widget. You must import ttk, and use ttk.Treeview.
@hiihihihelloo: look more closely at my answer. You're not importing ttk, you're simply importing tkinter and giving it a different name.
|
0

Why are you doing this?

import tkinter as tk
from tkinter import *
import tkinter as ttk

You first import tkinter as tk, then import the entire library, then import the library again but this time as ttk. So either import the library as a whole with *. Or choose an alias such as tk.

Additionnaly, I think you should try from tkinter import ttk and then call ttk.Treeview.

Be careful next time ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.