1

When I try to run this code, it is supposed to print the user input, but rather, it prints None every time , even though I am giving a value to the variable. What am I supposed to do? I don't get an error in the console either.

from tkinter import *
main_window= Tk()

#labels

Label(main_window, text="Name:").grid(row=0,column=0)
Label(main_window,text="Age:").grid(row=1,column=0)

#text input
name= Entry(main_window,width=30,borderwidth=1).grid(row =0,column=1)
#text input for Class
age= Entry(main_window,width=30,borderwidth=1,).grid(row =1,column=1)

#defines on click function
def on_click():
    print(f"Name = {name}, Age = {age}")  #prints none when clicked submit

#Button
Button(main_window,text="Submit",command =on_click).grid(row=2,column=1)


main_window.mainloop()

This is the Code

Output:

Name = None, Age = None
  • 2
    Does this answer your question? [Tkinter: AttributeError: NoneType object has no attribute ](https://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-attribute-name) – TheLizzard Aug 14 '21 at 09:38
  • In the question that I linked it said that if you use: `variable = Widget(...).grid(...)`, `variable` will be `None` and that you have to split it in 2 lines. You have the same problem. – TheLizzard Aug 14 '21 at 09:44
  • I tried that, but now it says: ` Name = .!entry Age = .!entry ` –  Aug 14 '21 at 09:48
  • @TheLizzard but as I recently discovered you can easily use walrus operator to put them in one line (Python3.8+) like so: `(variable := Widget(...)).grid(...)` then you can use `variable` and it won't be `None` (do I suggest? not in general code but can easily be used in list comprehensions or other loops otherwise it may get a bit hard to read them if you create everything using this) – Matiiss Aug 14 '21 at 09:53
  • @Matiiss I wouldn't suggest it because of 2 reasons: it's hard to keep track of all of those brackets and the 79/80 characters on a line PEP 8 recommendation. – TheLizzard Aug 14 '21 at 09:55
  • @Matiiss Also the `:=` is called [the walrus operator](https://docs.python.org/3/whatsnew/3.8.html#assignment-expressions) – TheLizzard Aug 14 '21 at 09:56
  • @TheLizzard isn't that what I said? _walrus operator_? also don't know about those brackets, for me personally I always write both before typing anything in them (IDE also helps with that). The char count however is not really an issue, you can easily split them in multiple lines, but I agree that with general use they are not that good, also I will add this to the answer you mentioned as duplicate – Matiiss Aug 14 '21 at 10:02
  • @Matiiss My bad. I didn't see that you wrote *walrus operator*. Also it's hard to read code when there are too many brackets and I am using IDLE, which has quite a few bracket related bugs. – TheLizzard Aug 14 '21 at 10:04

3 Answers3

0

Your code had a few bugs, mostly with the naming of widgets.

The Label widgets have been name so output will be sent to them.

The Entry widgets have been name so their contents can be extracted with the get method.

The function on_click now displays results.

import tkinter as tk

main_window = tk.Tk()

labelname = tk.Label(main_window, text="Name:", anchor="nw", justify="left")
labelname.grid(row=0, column=0, sticky="ew")
labelage = tk.Label(main_window, text="Age:", anchor="nw", justify="left")
labelage.grid(row=1, column=0, sticky="ew")

name =  tk.Entry(main_window, width=30)
name.grid(row=2, column=0)

age =  tk.Entry(main_window, width=30)
age.grid(row=3, column=0)

def on_click():
    labelname["text"] = f"Name:  {name.get()}"
    labelage["text"]  = f"Age:   {age.get()}"

tk.Button(main_window, text="Submit", command=on_click).grid(row=4, column=0)

main_window.mainloop()
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
Derek
  • 1,916
  • 2
  • 5
  • 15
0

This is because you have put .grid on the same line. Always try to write the layout type on the next line.This will save you lot of trouble in the future.

Try the modified code below.

from tkinter import *
from tkinter import ttk

main_window= Tk()

#labels
main_window.geometry("750x250")

Label(main_window, text="Name:").grid(row=0,column=0)
Label(main_window,text="Age:").grid(row=1,column=0)

#text input
my_name= ttk.Entry(main_window,width=30)
my_name.grid(row =0,column=1)
#text input for Class
my_age= ttk.Entry(main_window,width=30)
my_age.grid(row =1,column=1)

#defines on click function
def on_click():
    name = my_name.get()
    age = my_age.get()
    print(f"Name = {name}, Age = {age}")  #prints none when clicked submit

#Button
Button(main_window,text="Submit",command =on_click).grid(row=2,column=1)


main_window.mainloop()
Joseph
  • 1
0

Edit: Re-wrote script for Label widgets.

When I try to run this code, it is supposed to print the user input, but rather, it prints None every time , even though I am giving a value to the variable.

In case you're getting None, because you manipulated all in one line. You need to split two lines.

label_name and label_age, you must specify grid()

Use f-string format and add keyword .config in on_click function.

Snippet:

from tkinter import *
 

main_window = Tk()

#labels
main_window.geometry("750x250")

label_name = Label(main_window, text="Name:")
label_name.grid(row=3,column=0)

label_age = Label(main_window,text="Age: ")
label_age.grid(row=4,column=0)

#text input
name = Entry(main_window,width=30,borderwidth=1)
name.grid(row =0,column=1)
 
age= Entry(main_window,width=30,borderwidth=1,)
age.grid(row =1,column=1)

#defines on click function
def on_click():
    label_name.config(text=f'Name: {name.get()}')
    label_age.config(text=f'Age: {age.get()}')
     

#Button
Button(main_window,text="Submit",command=on_click).grid(row=2,column=1)


main_window.mainloop()

Screenshot:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19