TypeError:必须是实数,而不是Entry

时间:2018-07-02 17:26:20

标签: python tkinter

我正在创建用于创建计算器的代码,但我不断收到此错误:

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

有人知道为什么我的代码不起作用吗?我真的不明白为什么我不能收集和输入用户输入的内容?我正在研究,但无法弄清楚如何将用户的输入正确地合并到代码中。

这是我使用的代码:

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)

2 个答案:

答案 0 :(得分:1)

尝试将用户输入转换为floatint。似乎您正在尝试对用户输入(Entry对象)进行数学运算,但是Entry对象不支持数学运算。

答案 1 :(得分:1)

此代码存在很多问题:

  • 您需要存储这些get调用的结果。
  • As suggested by Joel,您需要将它们转换为floatint
  • 您应该在启动时一次创建结果Label,并在此回调中创建config文本,而不是每次用户点击Label时都创建一个新的Submit
  • 与其重复所有相同的代码6次,不如计算result链中的elif,然后在末尾使用它。

我在回答上一个问题时已经解释了其中的大部分内容。

结果应如下所示:

result_label =标签(窗口,文本= str(n_textentry ** m_textentry),bg =“白色”) result_label.grid(行= 13,列= 0,粘性= 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

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