-1

This my very first time interact with Tkinter and I'm trying to make a sign up form application but it it keep showing error when I tried to save the info into a txt file But the application do run it only get the error when I press the Button that was meant to save the info

Here the codes:

from tkinter import messagebox

main = Tk()
main.title('Sign up application')
main.resizable(0, 0)
main.config(bg='#CCCCCC')

genderr = StringVar()
genderr.set('Male')

Label(main, text='Name: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=1)
Label(main, text='Age: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=2)
Label(main, text='Email: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=3)
Label(main, text='Number: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=4)
Label(main, text='Gender: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=5)
Label(main, text='Password: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=6)
Label(main, text='Re-Enter Password: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=7)

tName=Entry(main, bg='#b7baa6', width=30).grid(column=1, row=1, padx=10, pady=10, sticky=E)
tAge=Entry(main, bg='#b7baa6', width=30).grid(column=1, row=2, padx=10, pady=10, sticky=E)
tEmail=Entry(main, bg='#b7baa6', width=30).grid(column=1, row=3, padx=10, pady=10, sticky=E)
tNumber=Entry(main, bg='#b7baa6', width=30  ).grid(column=1, row=4, padx=10, pady=10, sticky=E)

gender1=Radiobutton(main, activebackground='#d0e63e', text='Male', value='Male', variable=genderr).place(x=150, y=155)
gender2=Radiobutton(main, activebackground='#d0e63e', text='Female', value='Female', variable=genderr).place(x=260, y=155)

tPass=Entry(main, bg='#b7baa6', width=30, show=('*')).grid(column=1, row=6, padx=10, pady=10, sticky=E)
tRePass=Entry(main, bg='#b7baa6', width=30, show='*').grid(column=1, row=7, padx=10, pady=10, sticky=E)
def invalid():
    if tName.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!') 
        return
    elif tAge.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tRePass.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tPass.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tEmail.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tNumber.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tPass.get() != tRePass.get():
        messagebox.showerror('Error', 'Password did not match')
        return
    else:
        f = open('Info.txt', 'a')
        f.write('Name: ', tName.get(), '\nEmail: ', tEmail.get(), '\nNumber: ', tNumber.get(), '\nGender: ', genderr.get(), '\nPassword: ', tPass.get())
        f.close()
        messagebox.showinfo('Success', 'You have succesed sign up')
        return
But = Button(main, text='Sign up', command= invalid).grid(column=1, row=8)


main.mainloop()```


***PS C:\Users\Hp\Documents> & C:/Users/Hp/AppData/Local/Programs/Python/Python310/python.exe c:/Users/Hp/Documents/gui.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Hp\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\Users\Hp\Documents\gui.py", line 31, in invalid
    if tName.get() == '':
AttributeError: 'NoneType' object has no attribute 'get'***
Quang
  • 3
  • 1

2 Answers2

0

In Tkinter if the Position of the widget is done in the same line then it returns none if you do the position in the another link then it will object

tName=Entry(main, bg='#b7baa6', width=30).grid(column=1, row=1, padx=10, pady=10, sticky=E)

In the above line if you will print tName then it will print none but if you use the below method and then print tName then it will print the object name

tName=Entry(main, bg='#b7baa6', width=30) tName.grid(column=1, row=1, padx=10, pady=10, sticky=E)

I have attached the working code below

from tkinter import messagebox
from tkinter import Tk,StringVar,Entry,E,Radiobutton,Button
from tkinter import Label

main = Tk()
main.title('Sign up application')
main.resizable(0, 0)
main.config(bg='#CCCCCC')

genderr = StringVar()
genderr.set('Male')

Label(main, text='Name: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=1)
Label(main, text='Age: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=2)
Label(main, text='Email: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=3)
Label(main, text='Number: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=4)
Label(main, text='Gender: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=5)
Label(main, text='Password: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=6)
Label(main, text='Re-Enter Password: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=7)

tName=Entry(main, bg='#b7baa6', width=30)
tName.grid(column=1, row=1, padx=10, pady=10, sticky=E)

tAge=Entry(main, bg='#b7baa6', width=30)
tAge.grid(column=1, row=2, padx=10, pady=10, sticky=E)

tEmail=Entry(main, bg='#b7baa6', width=30)
tEmail.grid(column=1, row=3, padx=10, pady=10, sticky=E)

tNumber=Entry(main, bg='#b7baa6', width=30  )
tNumber.grid(column=1, row=4, padx=10, pady=10, sticky=E)

gender1=Radiobutton(main, activebackground='#d0e63e', text='Male', value='Male', variable=genderr).place(x=150, y=155)
gender2=Radiobutton(main, activebackground='#d0e63e', text='Female', value='Female', variable=genderr).place(x=260, y=155)

tPass=Entry(main, bg='#b7baa6', width=30, show=('*'))
tPass.grid(column=1, row=6, padx=10, pady=10, sticky=E)

tRePass=Entry(main, bg='#b7baa6', width=30, show='*')
tRePass.grid(column=1, row=7, padx=10, pady=10, sticky=E)
def invalid():
    if tName.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!') 
        return
    elif tAge.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tRePass.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tPass.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tEmail.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tNumber.get() == '':
        messagebox.showerror('Error', 'You need to complete the form!')
        return
    elif tPass.get() != tRePass.get():
        messagebox.showerror('Error', 'Password did not match')
        return
    else:
        f = open('Info.txt', 'a')
        f.write('Name: '+str( tName.get())+ '\nEmail: '+str( tEmail.get())+ '\nNumber: '+str( tNumber.get())+ '\nGender: '+str( genderr.get())+ '\nPassword: '+str(tPass.get()))
        f.close()
        messagebox.showinfo('Success', 'You have succesed sign up')
        return
But = Button(main, text='Sign up', command= invalid).grid(column=1, row=8)


main.mainloop()
0

AttributeError: 'NoneType' object has no attribute 'get' This means that tName is NoneType The problem here is that you are calling a function that returns None after instantiating the class Label, this is on line 11::

Label(main, text='Name: ', bg='#CCCCCC', font=('Times', 12)).grid(column=0, row=1)

If you see in the source code of the function grid, it returns None::

    def grid_configure(self, cnf={}, **kw):
        """Position a widget in the parent widget in a grid. Use as options:
        column=number - use cell identified with given column (starting with 0)
        columnspan=number - this widget will span several columns
        in=master - use master to contain this widget
        in_=master - see 'in' option description
        ipadx=amount - add internal padding in x direction
        ipady=amount - add internal padding in y direction
        padx=amount - add padding in x direction
        pady=amount - add padding in y direction
        row=number - use cell identified with given row (starting with 0)
        rowspan=number - this widget will span several rows
        sticky=NSEW - if cell is larger on which sides will this
                      widget stick to the cell boundary
        """
        self.tk.call(
              ('grid', 'configure', self._w)
              + self._options(cnf, kw))

    grid = configure = config = grid_configure