3

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?

hiihihihelloo
  • 99
  • 2
  • 8

2 Answers2

6

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(...)
...
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks for the reply, i tried `import tkinter as ttk` which still gave me the same error. – hiihihihelloo Sep 30 '18 at 11:34
  • @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. – Bryan Oakley Sep 30 '18 at 12:32
  • 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 Sep 30 '18 at 15:33
  • @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`. – Bryan Oakley Sep 30 '18 at 19:29
  • bro, i did and i got the exact same error. I'll go ahead and update my current posts code to show you. – hiihihihelloo Sep 30 '18 at 21:10
  • 1
    @hiihihihelloo: look more closely at my answer. You're not importing ttk, you're simply importing tkinter and giving it a different name. – Bryan Oakley Sep 30 '18 at 21:40
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 ;)

Chris Ze Third
  • 632
  • 2
  • 18