tkinter .get 方法无法正常工作

The tkinter .get metho d is not working properly

我正在尝试为学校项目制作公式计算器。我正在尝试使用 .get tkinter 方法来获取条目中的内容。它总是发送一个错误。不过我不想把它写成 class。

这不是最终代码。

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)

当我执行代码时,它在控制台上显示:

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'

您不能在与初始化相同的行上使用 gridpack 这样的布局。你必须把它们放在不同的行上:

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

当您将小部件分配给变量时,不要直接在同一行上调用小部件上的布局管理器方法;在另一条线上做。
原因是更薄的布局管理器 packgridplace return None

from tkinter import *

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

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

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