在tkinter中输出一个等式

时间:2015-03-01 19:26:51

标签: python tkinter

我正在尝试创建一个简单的计算器,但我还必须显示方程并在tkinter上回答。我唯一能做的就是得到在tkinter中显示的答案,而不是等式和答案。与a + b = c一样。

from tkinter import *
import time 

master = Tk()

canvas_width = 400
canvas_height = 400

w = Canvas(master, width=canvas_width, height=canvas_height, bg="#B7F9E3")
w.pack()

a = int(input("enter first operand: "))
operation = input("enter operator: ")
b = int(input("enter second operand: "))
d = 

def add(a,b):
    c = a + b
    print(c)

def subtract(a, b):
    c = a - b
    print(c)

def division(a, b):
    c = a / b
    print(c)

def multiply(a, b):
    c = a * b
    print(c)

if operation == "+" or operation == "+":
    add(a, b)

if operation == "-" or operation == "-":
    subtract(a, b)

if operation == "/" or operation == "/":
    division(a, b)

if operation == "*" or operation == "*":
    multiply(a, b)

if operation == "+":
    w.create_text(100, 50, font=("times new roman", 16), text="Addition Results")
    w.create_text(200, 200, font=("times new roman", 16), text=d)

if operation == "-":
    w.create_text(100, 50, font=("times new roman", 16), text="Subtraction Results")
    w.create_text(200, 200, font=("times new roman", 16), text=d)

if operation == "*":
    w.create_text(100, 50, font=("times new roman", 16), text="Multiplication Results")
    w.create_text(200, 200, font=("times new roman", 16), text=d)

if operation == "/":
    w.create_text(100, 50, font=("times new roman", 16), text="Division Results")
    w.create_text(200, 200, font=("times new roman", 16), text=d)

1 个答案:

答案 0 :(得分:1)

您需要分配变量d

尝试将功能定义更改为:

def add(a,b):
    c = a + b
    return str(c)

if语句是这样的:

if operation == "+":
    d = add(a,b)

你可以这样计算:

x = 1
y = 2
operation = "+"
equation = str(x) + operation + str(y)
print(equation)
# 1+2