Farenheit到Deg Celcius转换器的GUI窗口执行中没有类型错误

时间:2018-08-06 09:26:40

标签: python python-3.x tkinter tkinter-entry

from tkinter import *
my_window = Tk()
def converter():
    F = float(entry_input.get())
    T = (F-32)*5/9
    display_Temp["text"] = str(T) 
Label(my_window,text="Enter Temperature in Farenheit = ").grid(row=0,column=0)
display_Temp = Label(my_window).grid(row=1,column=1)
entry_input = Entry(my_window).grid(row=0,column=1)
button = Button(my_window,text="Convert to Deg Celcius",command = converter,bd=8,relief="raised").grid(row=1,column=0)
my_window.mainloop()

我在GUI窗口中输入了20 F,该窗口的创建是将华氏值转换为摄氏度。但是,当我按下转换器按钮时,它给出以下错误消息:AttributeError:'NoneType'对象没有属性'get'

请参考图片以了解错误

enter image description here 我已按照以下接受的答案包含了更正,它的工作原理如下: enter image description here

1 个答案:

答案 0 :(得分:0)

几何管理器(gridpackplace)返回None;您不应与grid小部件分配同一行。

import tkinter as tk     # <-- avoid star imports


def converter():
    F = float(entry_input.get())
    T = (F - 32) * 5 / 9
    display_Temp["text"] = str(T) 


my_window = tk.Tk()

tk.Label(my_window, text="Enter Temperature in Farenheit = ").grid(row=0, column=0)

display_Temp = tk.Label(my_window)
display_Temp.grid(row=1, column=1)

entry_input = tk.Entry(my_window)
entry_input.grid(row=0, column=1)

button = tk.Button(my_window, text="Convert to Deg Celcius", command=converter, bd=8, relief="raised")
button.grid(row=1, column=0)

my_window.mainloop()