嘿伙计们我正在尝试为一个小程序编写一个代码并且一切正常但我无法在Canvas中显示我的整个数学等式(C = A + B)或其任何变体。这是我的下面的代码,任何帮助将不胜感激。用户输入他们的第一个数字,然后输入运算符(+, - ,*,/),然后输入第二个数字,然后输入"加法结果,减法结果,乘法结果或除法结果"显示文本以及生成的数字,运算符和结果答案。我的结果"例如,如果我输入60 + 5作为我的数字和操作符,Canvas仅显示" Addition Result"我想要的地方但不是60 + 5 = 65.这是我需要帮助的地方!
from tkinter import *
master = Tk()
canvas_width = 400
canvas_height = 400
w = Canvas (master, width=canvas_width, height=canvas_height, bg = "blue")
w.pack()
A = int(input("Enter the First Operand: "))
O = (input("Enter the Operator: "))
B = int(input("Enter the Second Operand: "))
#If Functions for Math
def Add(A, B):
return A + B
def Subtract(A, B):
return A - B
def Multiply(A, B):
return A * B
def Divide(A, B):
return A / B
if (O == "+"):
Add(A, B)
elif (O == "-"):
Subtract(A, B)
elif (O == "*"):
Multiply(A, B)
else:
if (O == "/"):
Divide(A, B)
#If Functions for Formula
if (O == "+"):
D = Add(A, B)
elif (O == "-"):
D = Subtract(A, B)
elif (O == "*"):
D = Multiply(A, B)
else:
if (O == "/"):
D = Divide(A, B)
#If Functions for Result text
if (O == "+"):
w.create_text(canvas_width / 3, canvas_height / 3, font=("Arial", 14), text="Addition Result", fill="white")
elif (O == "-"):
w.create_text(canvas_width / 3, canvas_height / 3, font=("Arial", 14), text="Subtraction Result", fill="white")
elif (O == "*"):
w.create_text(canvas_width / 3, canvas_height / 3, font=("Arial", 14), text="Multiplication Result", fill="white")
else:
if (O == "/"):
w.create_text(canvas_width / 3, canvas_height / 3, font=("Arial", 14), text="Division Result", fill="white")
#If Functions for Formula Text
if (O == "+"):
w.create_text(canvas_width / 2, canvas_height / 2, font=("Arial", 18), text=D, fill="white")
elif (O == "-"):
w.create_text(canvas_width / 2, canvas_height / 2, font=("Arial", 18), text=D, fill="white")
elif (O == "*"):
w.create_text(canvas_width / 2, canvas_height / 2, font=("Arial", 18), text=D, fill="white")
else:
if (O == "/"):
w.create_text(canvas_width / 2, canvas_height / 2, font=("Arial", 18), text=D, fill="white")
答案 0 :(得分:1)
您的功能Add
,Subtract
,Multiply
,Divide
不会返回任何内容,因此当您致电D = Add(1 + 1)
时,D为无。所以只需添加return
而不是C =
:
def Add(A, B):
return A + B
def Subtract(A, B):
return A - B
def Multiply(A, B):
return A * B
def Divide(A, B):
return A / B
编辑:如果您不想说“1 + 1 = 2”,请将text = D
替换为text = "{} {} {} = {}".format(A, O, B, D)