简体   繁体   English

如何使用 tkinter 模块使用在 Python 的“条目”中输入的变量创建一个按钮来执行和显示计算?

[英]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:就我的做法而言,我仍然需要在控制台而不是在入口块中输入xy的值,然后单击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.

相关问题 如何使用来自 tk.Entry 的变量将 tkinter 按钮与 function 一起使用? - How do I use a tkinter button with a function, using variables from tk.Entry? Tkinter,Python:如何保存在“输入”小部件中输入的文本? 如何移动标签? - Tkinter, Python: How do I save text entered in the Entry widget? How do I move a label? 如何使用 tkinter 上的图标图像制作主页按钮? - How do I make a home button using an image for the icon on tkinter? 如何使用tkinter for python中的迭代创建输入字段和按钮以从输入字段获取内容? - how do I create entry fields and buttons to get the content from the entry field using iteration in tkinter for python? 在Python 2.6中使用Tkinter,如何使按钮小部件在文本框中打印出选中的Checkbuttons? - Using Tkinter in Python 2.6 how do I make a button widget print out selected Checkbuttons in a text box? 如何在python中使用tkinter将文本框条目传递给变量 - How to pass a textbox entry to variables using tkinter in python 如何自动使 python tkinter Entry 自动进行? - How do I automatically make python tkinter Entry automatically? 如何使用 tkinter 中的按钮调用单独的 python 程序? - How do I call a separate python program using a button in tkinter? 如何制作Tkinter条目小部件? - How do I make a tkinter entry widget? 如何使用tkinter条目而不是声明字符串或值从python中的SQLite数据库中删除? - How do I delete from a SQLite database in python using a tkinter entry instead of stating a string or value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM