python - 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()
解决方案
好的,这是一个非常常见的错误。当你创建你的时候,Entry
你这样做:
variable = Entry(...).place(...)
这将创建条目,然后立即调用其.place
方法。它存储任何.place
返回(总是None
)到variable
. 你想要做的是:
variable = tk.Entry(...)
variable.place(...)
推荐阅读
- python - 如何按字母顺序组织列表中的列表?
- r - 从 Github 安装的软件包是否需要 Rtools?
- sql - 涉及 SELECT FOR UPDATE 的死锁
- github - 如何从 github graphql 查询中获取拉取请求的默认合并方法?
- c++ - 查找由数字 0 和 1 组成的数字“m”的算法说明,该数字可以被数字 n 整除
- r - Shiny:从 fileInput 获取准确的数据路径,而不是临时数据路径
- mysql - MS 通过 odbc 访问 mySql 连接慢
- typescript - 使用静态成员定义类型参数
- java - JAX-RS 2.1.1 响应构建抛出错误
- php - 如何使用 PHP 检测按钮单击并打开新页面?