Fahadkalis
Fahadkalis

Reputation: 3261

ImportError: cannot import name 'Tk'

I am writing a program in python on ubuntu, but since couple of days I am facing this problem which is very irratating,

Traceback (most recent call last):
  File "/home/tansen/Documents/python2/button25_01JanV1.py", line 1, in <module>
    import Tkinter,ttk
  File "/home/tansen/Documents/python2/Tkinter.py", line 1, in <module>
    from Tkinter import Tk, Label, Button
ImportError: cannot import name 'Tk'

before that this program was running OK, and I run several time program successfully

import tkinter.ttk 
from tkinter import *


def viewFile():

    pass

if __name__ == '__main__':

    root = Tk()


    step = LabelFrame(root,text="FILE MANAGER", font = "Arial 20 bold   italic")
    step.grid(row=1, columnspan=7, sticky='W',padx=100, pady=5, ipadx=130, ipady=25)

    Button(step,    text="OpenFile",    font = "Arial 8 bold    italic",    activebackground="turquoise",   width=30, height=5, command=viewFile).grid      (row= 6, column =3)
    Button(step,    text="Exit",        font = "Arial 8 bold    italic",    activebackground="turquoise",   width=20, height=5, command=root.quit).grid     (row= 6, column =5)

    tex = Text(master=root)                                                                 # TextBox For Displaying File Information
    scr=Scrollbar(root,orient =VERTICAL,command=tex.yview)
    scr.grid(row=8, column=2, rowspan=15, columnspan=1, sticky=NS)
    tex.grid(row=8, column=1, sticky=E)
    tex.config(yscrollcommand=scr.set,font=('Arial', 8, 'bold', 'italic'))

    root.mainloop()

As advice by the experts I rename the file Tkinter.py and change the caps from 'T' to 't'. After that my program is running successfully but another issue has created. Pressing of exit button is not working.

enter image description here

Can you please help to get rid from this problem successfully.

Upvotes: 0

Views: 14268

Answers (2)

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

You called a file Tkinter.py in the same directory, rename it and delete the .pyc

/home/tansen/Documents/python2/Tkinter.py <- importing from this not the module

You are also using the import syntax for python 2's Tkinter, use:

from tkinter import ttk, Text, Button, LabelFrame, VERTICAL, E, NS, Scrollbar, Tk

Upvotes: 5

awesomeshreyo
awesomeshreyo

Reputation: 15

Make sure the file you are importing is in the same folder as your program. Then, delete the pycache folder (or whatever it is called on Ubuntu, I'm using Windows. It will be a "Compiled Python File"). This could have corrupted annd stopped your program from working properly. If this doesn't work, then I'm afraid I'm out of ideas.

Upvotes: 0

Related Questions