[英]How do I make a button to execute and display calculation using variables entered in “Entry” in Python using the tkinter module?
I want to be able to execute the calculation x + y
when the button SOLVE
is click.我希望能够在单击按钮SOLVE
时执行计算x + y
。
With how I have done it, I still have to input values for x
and y
in the console instead of in the entry block and upon clicking the SOLVE
button, it returns:就我的做法而言,我仍然需要在控制台而不是在入口块中输入x
和y
的值,然后单击SOLVE
按钮,它会返回:
"TypeError: unsupported operand type(s) for +: 'NoneType' and 'NoneType' “TypeError:+ 的不支持的操作数类型:‘NoneType’和‘NoneType’
Code:代码:
import tkinter
from tkinter import *
window = tkinter.Tk()
window.title("SOLVE MATH")
def solve_now():
x= tkinter.Label(window, text="X").pack()
X = tkinter.Entry(window, text=int(input("Enter X value:
"))).pack()
y = tkinter.Label(window, text="Y").pack()
Y = tkinter.Entry(window, text=int(input("Enter Y value:
"))).pack()
ans = X + Y
tkinter.Label(window, text=ans).pack()
tkinter.Button(window, text="SOLVE",
command=solve_now).pack()
window.mainloop()
This how to get values in tkinter entry
to do calculation on it.For you to achieve that you have to create entry widget
then use get
function to retrieve the value in the entry to do your calculations.这是如何获取tkinter entry
中的值以对其进行计算。为此,您必须创建entry widget
,然后使用get
function 检索条目中的值以进行计算。 Read more about entry widget here entry widget在此处阅读有关条目小部件的更多信息条目小部件
Using input
will let you type you value in the console.使用input
将让您在控制台中键入您的值。 You are getting Nonetype error
because you have to position your geometry manager pack
on the next line after the entry widget
function.您收到Nonetype error
,因为您必须在entry widget
function 之后的下一行中将您的几何管理器pack
position。
entry1 = Entry(window )
entry1.pack()
Full code完整代码
import tkinter
from tkinter import *
def solve_now():
ans = float(float(entry1.get()) + float(entry2.get()))
print(ans)
l3.config(text="Answer : "+str(ans))
window = tkinter.Tk()
window.title("SOLVE MATH")
l1 = Label(window, text="Enter Value X")
l1.pack()
entry1 = Entry(window, )
entry1.pack()
l2 = Label(window, text="Enter value Y")
l2.pack()
entry2 = Entry(window)
entry2.pack()
b1 = Button(window, text="SOLVE",
command=solve_now)
b1.pack()
l3 = Label(window)
l3.pack()
window.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.