类型错误:必须是实数,而不是条目 [英] TypeError: must be real number, not Entry

查看:44
本文介绍了类型错误:必须是实数,而不是条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个代码来创建一个计算器,但我一直收到这个错误:

I am creating a code to create a calculator but I keep on getting this error:

Traceback (most recent call last):
     File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36- 
  32\lib\tkinter\__init__.py", line 1702, in __call__
       return self.func(*args)
     File "C:\Users\Monish Shah\AppData\Local\Programs\Python\Python36- 
 32\monish-play\calc-completed-copy-for-editing-copy2.py", line 40, in click
Label (window, text = str(sqrt(n_textentry)), bg = "white") .grid(row = 13, 
  column = 0, sticky = N)
    TypeError: must be real number, not Entry

有谁知道为什么我的代码不起作用?我真的不明白为什么它不能是和条目,因为我正在收集用户的输入?我正在研究,但我无法弄清楚如何将用户的输入正确地合并到代码中.

Does anyone know why my code does not work? I don't really understand why it cannot be and entry since I am collecting the user's input? I was researching but I could not figure out how to correctly incorporate the user's input into the code.

这是我使用的代码:

from math import sqrt
from tkinter import *

window = Tk()
window.title("Welcome to Calculator ")
window.configure(background = "white")
Label (window, text = "Calculator", bg = "white") .grid(row = 0, column = 0, 
sticky = N)


#to create the box for the first number and store it
Label (window, text = "Enter the first number", bg = "white") .grid(row = 1, 
column = 0, sticky = N)
n_textentry = Entry(window, width = 10, bg = "white")
n_textentry.grid(row = 2, column = 0, sticky = N)

#to create the box for the second number
Label (window, text = "Enter the second number", bg = "white") .grid(row = 5, 
column = 0, sticky = N)
m_textentry = Entry(window, width = 10, bg = "white")
m_textentry.grid(row = 6, column = 0, sticky = N)


#click function
def click():
    n_textentry.get()

    m_textentry.get()

    operation_textentry.get()

    if operation_textentry == 1:
        result1 = Label (window, text = str(n_textentry + m_textentry), bg = 
"white") .grid(row = 13, column = 0, sticky = N)
    elif operation_textentry == 2:
         Label (window, text = str(n_textentry - m_textentry), bg = "white") 
.grid(row = 13, column = 0, sticky = N)
    elif operation_textentry == 3:
         Label (window, text = str(n_textentry * m_textentry), bg = "white") 
.grid(row = 13, column = 0, sticky = N)
    elif operation_textentry == 4:
         Label (window, text = str(n_textentry / m_textentry), bg = "white") 
.grid(row = 13, column = 0, sticky = N)
    elif operation_textentry == 5:
         Label (window, text = str(n_textentry ** m_textentry), bg = "white") 
.grid(row = 13, column = 0, sticky = N)
    else:
         Label (window, text = str(sqrt(n_textentry)), bg = "white") 
.grid(row = 13, column = 0, sticky = N)


   # operation_textentry == 6:
     #   Label (window, text = str(sqrt(n_textentry)), bg = "white") 
.grid(row = 13, column = 0, sticky = N)
    #else:
     #   print("Invalid Operation ")




#to show list of options
Label (window, text = '''
Enter 1 for addition
Enter 2 for subtraction
Enter 3 for multiplication
Enter 4 for division
Enter 5 for exponentiation
Enter 6 for square root *This will only work for 1st choice*''', bg = 
"white") .grid(row = 9, column = 0, sticky = W)

operation_textentry = Entry(window, width = 10, bg = "white")
operation_textentry.grid(row = 10, column = 0, sticky = N)
Button(window, text = "Submit", width = 6, command=click) .grid(row = 11, 
column = 0, sticky = N)

推荐答案

这段代码有很多问题:

  • 您需要存储那些 get 调用的结果.
  • 按照 Joel 的建议,您需要将它们转换为 floatint.
  • 你应该在启动时创建一次结果Label,并config这个回调中的文本,而不是每次都创建一个新的Label用户点击Submit.
  • 不需要重复所有相同的代码 6 次,只需在 elif 链中计算一个 result,然后在最后使用它.
  • You need to store the results of those get calls.
  • As suggested by Joel, you need to convert them to float or int.
  • You should create the results Label once at startup, and config the text in this callback, instead of creating a new Label every time the user hits Submit.
  • Instead of repeating all of the same code 6 times, just calculate a result in the elif chain, and then use it at the end.

我已经在我对您上一个问题的回答中解释了大部分内容.

I already explained most of this in my answer to your previous question.

结果应该是这样的:

result_label = Label(window, text = str(n_textentry ** m_textentry), bg = "white")result_label.grid(row = 13, column = 0,sticky = N)

result_label = Label(window, text = str(n_textentry ** m_textentry), bg = "white") result_label.grid(row = 13, column = 0, sticky = N)

def click():
    n = int(n_textentry.get())
    m = int(m_textentry.get())
    operation = int(operation_textentry.get())

    if operation == 1:
        result = n+m
    elif operation == 2:
        result = n-m
    elif operation == 3:
        result = n*m
    elif operation == 4:
        result = n/m
    elif operation == 5:
        result = n**m
    else:
        result = "Invalid Operation"
    result_label.config(text=str(result))

正如我之前提到的,对于用户将其中一个条目留空,或输入文本而不是数字,或除以零等情况,您可能需要一些错误处理.最简单的方法是使用try: 围绕整个click 功能:

As I mentioned before, you probably want some error handling for the case where the user leaves one of the entries blank, or inputs text instead of a number, or divides by zero, etc. The simplest way to do this with a try: around the whole click function:

def click():
    try:
        n = int(n_textentry.get())
        # etc.
    except Exception as e:
        result_label.config(text=repr(e))

这篇关于类型错误:必须是实数,而不是条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆