-1

I'm making a GUI using Tkinter which allows data to be entered in one of the windows:

enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore).pack()

I am trying to make a button which will save the entered data as a variable:

savebttn = Button(window4, text= "Save", width=5, height=2, font="Helvatica 25", command = savescore).pack()

Where:

def savescore():
    score = enterscore.get()

However, if I run the program it returns the error message:

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

Where am I going wrong?

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
Oceanescence
  • 1,967
  • 3
  • 18
  • 28

2 Answers2

3

Don't do this:

enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore).pack()

Calling pack returns None, not the widget. tkinter does not support that sort of method chaining. Instead do:

enterscore = Entry(window4, font="Helvatica 25", textvariable = newscore)
enterscore.pack()

This constructs the widget, and sets enterscore to point at it. Calling enterscore.pack then has the same effect as above, but eneterscore is left pointing at the correct object (the widget, not None)

matsjoyce
  • 5,744
  • 6
  • 31
  • 38
1

pack method returns None. You need to separate the statement that create the Entry and the statemetn that pack.

enterscore = Entry(window4, font="Helvatica 25", textvariable=newscore)
enterscore.pack()

Same for the savebttn.

falsetru
  • 357,413
  • 63
  • 732
  • 636