Saket Mittal
Saket Mittal

Reputation: 3906

AttributeError: class Tk has no attribute 'tk'

hey guys i stuck on simple python GUI program

import Tkinter as tk

window = tk.Tk
text_box = tk.Entry(window)


def save_text():
    str1 = text_box.get()
    fx = open("file1.txt", "w")
    fx.write(str1)
    fx.close()

btn1 = tk.Button(window, text="Save", command="save_text")
text_box.pack()
btn1.pack()
window.mainloop()

i this error:

Traceback (most recent call last):
  File "C:/Users/Saket/PycharmProjects/guiform1/firstform.py", line 5, in <module>
    text_box = tk.Entry(window)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2385, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
    BaseWidget._setup(self, master, cnf)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
    self.tk = master.tk
AttributeError: class Tk has no attribute 'tk'

Anybody having idea what am i doing wrong please help me??

Upvotes: 0

Views: 4345

Answers (1)

TigerhawkT3
TigerhawkT3

Reputation: 49330

You have to call tk.Tk to create an instance:

window = tk.Tk()
              ^^

You will then have a tk.Tk-type object to interact with.

Upvotes: 3

Related Questions