0
from tkinter import *

def login():
    root = Tk()
    root.resizable(False, False)
    root.title("Log in")
    root.geometry("300x200")
    root.configure(bg='#AFFDFF')

    def submit(usernameEntry):
        with open("username.txt", "w") as myFile:
            myFile.write(usernameEntry.get())

    Label(root, text="Please enter your username", bg='#AFFDFF').place(relx=0.5, rely=0.35, 
        anchor=CENTER)
    usernameEntry = Entry(root, width=25).place(relx=0.5, rely=0.5, anchor=CENTER)
    submitBtn = Button(root, text="Submit", bg='#A4FFCB', command=lambda: 
        submit(usernameEntry)).place(relx=0.5, rely=0.65, anchor=CENTER)

    root.mainloop()

My output is:

Traceback (most recent call last):
  File "C:\Users\george\anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "c:\Users\george\Desktop\rock paper scissors\interface.py", line 48, in <lambda>
    submitBtn = Button(root, text="Submit", bg='#A4FFCB', command=lambda: 
    submit(usernameEntry)).place(relx=0.5, rely=0.65, anchor=CENTER)
  File "c:\Users\george\Desktop\rock paper scissors\interface.py", line 44, in submit
    myFile.write(usernameEntry.get())
AttributeError: 'NoneType' object has no attribute 'get'

Please help me with this. I'm stuck for a while now. I have also tried not referancing 'usernameEntry' to submit function and it didn't work.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19
  • When providing code snippet please provide a code that can run on its own. Your code was missing these lines: `from tkinter import Tk, Label, Entry, Button, CENTER` and `root = Tk()` – Aidis Jan 31 '21 at 11:49

1 Answers1

0

As per the link that @acw1668 shared, .place in your code returns None, not the entry object, so your line:

usernameEntry = Entry(root, width=25).place(relx=0.5, rely=0.5, anchor=CENTER)

means that usernameEntry = None so that line needs to become:

usernameEntry = Entry(root, width=25)
usernameEntry.place(relx=0.5, rely=0.5, anchor=CENTER)

This is the same for your other variables, for example:

submitBtn = Button(root, text="Submit", bg='#A4FFCB', command=lambda: submit(usernameEntry)).place(relx=0.5, rely=0.65, anchor=CENTER)

should be:

submitBtn = Button(root, text="Submit", bg='#A4FFCB', command=lambda: submit(usernameEntry))
submitBtn.place(relx=0.5, rely=0.65, anchor=CENTER)
Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19