Search code examples
pythontkintertkinter-entry

The tkinter .get metho d is not working properly


I am trying to make a formula calculator for a school project. I am trying to use the .get tkinter method to get what is in an Entry. It always sends an error. I don't want to write it into a class though.

This is not the final code.

from tkinter import *

def speedCalc():
    _distance = spDistance.get()
    _time = spTime.get()

spDistance = Entry(speed).grid(row=1, column=1)
spTime = Entry(speed).grid(row=2, column=1)
spSpeed = Entry(speed).grid(row=3, column=1)

spConvert = Button(speed, text="Calculate", command=speedCalc)
spConvert.grid(row=4, column=1)

When I execute the code, it says this on the console:

Exception in Tkinter callback
Traceback (most recent call last):
  File"C:\Users\JackP\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
  File "C:/Users/JackP/Desktop/Python Projets/Formula App/4. Extention.py", line 25, in speedCalc
_distance = spDistance.get()
AttributeError: 'NoneType' object has no attribute 'get'

Solution

  • You can't use a layout like grid or pack on the same line as the initialization. You have to put those on separate lines:

    spDistance = Entry(speed)
    spDistance.grid(row=1, column=1)