首页 > 解决方案 > Tkinter 从值中获取数字

问题描述

我为一个程序编写了下面的代码,该程序计算我们用于二次方程的“增量”。我已经写了一个计算器,它从这样的条目中获取数值:firstnum=entry.get().

但是,当我尝试在 delta 上执行相同操作时,出现错误:

AttributeError:“NoneType”对象没有属性“get”

from tkinter import
from tkinter.font import Font
import sys*
    
# window interface
r = Tk()
r.geometry("300x500")
r.configure(bg="#414d61")
   
    
# commands
def clear():
    r.quit()
    
    
def delta():
    ax = entry_ax.get()
    bx = entry_bx.get()
    c = entry_c.get()
    result = bx * bx - 4 * ax * c
    entry_delta.insert(0, result)
    
    
# text interface
text = Text(r)
My_font = Font(family="Arial Rounded MT Bold", size=12)
    
# labels and frames
label_top = Label(r, bg="#414d61", text="x²    +  (", font=My_font).place(x=80, y=200)
label_top2 = Label(r, bg="#414d61", text="    x)    +", font=My_font).place(x=140, y=200)
label_top3 = Label(r, bg="#414d61", text="    =0", font=My_font).place(x=200, y=200)
entry_ax = Entry(r, bg="white", width=2).place(x=60, y=202)
entry_bx = Entry(r, bg="white", width=2).place(x=138, y=202)
entry_c = Entry(r, bg="white", width=2).place(x=198, y=202)
entry_delta = Entry(r, bg="white", width=20).place(x=85, y=350)
# Buttons
b1 = Button(r, bg="#414d61", text="Calculate Delta", font=My_font, command=delta, height=1, width=20)
b1.place(x=40, y=300)
b2 = Button(r, bg="#414d61", text="Exit", font=My_font, command=clear, height=1, width=20)
b2.place(x=40, y=400)
    
# entries
entry = Label(r, bg="#414d61", text="Delta Calculator (basic gui version)", font=My_font, width=30)
entry.place(x=5, y=100)
    
r.mainloop()

标签: pythonpython-3.xtkintertkinter-entry

解决方案


好的,这是一个非常常见的错误。当你创建你的时候,Entry你这样做:

variable = Entry(...).place(...)

这将创建条目,然后立即调用其.place方法。它存储任何.place返回(总是None)到variable. 你想要做的是:

variable = tk.Entry(...)
variable.place(...)

推荐阅读