-2

This is a very dumb project to test my absolute beginner skills in python 3.8. But I get the following error:

AttributeError: 'NoneType' object has no attribute 'get'

I don't know what's wrong.

import tkinter as tk

window = tk.Tk()
window.title('File Writer')

def getfile():
    text1 = entry1.get()
    text2 = entry2.get()
    text3 = entry3.get()
    text4 = entry4.get()
    fl = fl.get
    save = open('C:\\Users\\Anas\\Desktop\\' + fl + '.txt', 'w')
    save.write('Your name: ' +  text1 + '\n')
    save.write('Your Birthdate: ' + text2 + '\n')
    save.write('Your City: ' + text3 + '\n')
    save.write('Your Work: ' + text4 + '\n')
    save.close()


tk.Label(window, text='Welcome to file writer', font="Times 22 bold").pack()
tk.Label(window, text="What is your full name?").pack()
entry1 = tk.Entry(window).pack()
tk.Label(window, text="When is your birthdate (DD/MM/YYYY").pack()
entry2 = tk.Entry(window).pack()
tk.Label(window, text="Where do you live? (City, State, Country").pack()
entry3 = tk.Entry(window).pack()
tk.Label(window, text="What is your job? (position/company)").pack()
entry4 = tk.Entry(window).pack()
tk.Label(window, text='What do you want to name your file').pack()
entry5 = tk.Entry(window).pack()
tk.Button(window, text="Click to save your file", font='Arial 20', command=getfile).pack()


window.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

When you defined the entry, you put the .pack() method, but pack, grid or place don't return the element (tk.Entry), but None, so you when you defined the element with the .pack() in the end,

entry1 = tk.Entry(window).pack() # .pack() -> returns None

the value of the variable entry1 was set to None

To solve that you can define the variable first and then add the pack

entry1 = tk.Entry(window) # now the element is stored in the variable
entry1.pack()

and I suggest you change the path C:\\Users\\Anas\\Desktop\\

to an input from the user just so it can be available for other computers or do something like this

import getpass
path = f'C:\\Users\\{getpass.getuser()}\\Desktop\\'
n.qber
  • 354
  • 2
  • 8