首页 > 解决方案 > 我想在 Tkinter 中看到什么

问题描述

我用 python 和 Tkinter 做了一个计算器。当我点击答案按钮时,我只能看到结果,但我想看看什么 (+*/-) 等于这样的东西:

当我点击答案时我看到了什么:56

我想看到的:7*8 = 56

谁能回答?

这是我的代码:

#written by Amirali Rezaei
from Tkinter import *

window = Tk()
window.title("Yasan-Calculator")

e = Entry(window, width=40, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, padx=10, pady=10)

#e.insert(0, "Enter Your Name: ")

def button_click(number):
    #e.delete(0, END)
    current = e.get()
    e.delete(0, END)
    e.insert(0, str(current) + str(number))

def clearing():
    e.delete(0, END)

def adding():
    global f_num
    global math
    math = "ezafe"
    first_number = e.get()
    f_num = int(first_number)
    e.delete(0, END)

def divisioning():
    global f_num
    global math
    math = "taqsim"
    first_number = e.get()
    f_num = int(first_number)
    e.delete(0, END)

def zarb():
    global f_num
    global math
    math = "zarbkardan"
    first_number = e.get()
    f_num = int(first_number)
    e.delete(0, END)

def menha():
    global f_num
    global math
    math = "minus"
    first_number = e.get()
    f_num = int(first_number)
    e.delete(0, END)

def calculate():
    if(math == "ezafe"):
        second_number = e.get()
        e.delete(0, END)
        e.insert(0, f_num + int(second_number))

    if (math == "zarbkardan"):
        second_number = e.get()
        e.delete(0, END)
        e.insert(0, f_num * int(second_number))

    if(math == "minus"):
        second_number = e.get()
        e.delete(0, END)
        e.insert(0, f_num - int(second_number))

    if(math == "taqsim"):
        second_number = e.get()
        e.delete(0, END)
        e.insert(0, f_num / int(second_number))

#Define buttons
button_1 = Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(window, text="+", padx=39, pady=20, command=adding)
button_equal = Button(window, text="=", padx=91, pady=20, command=calculate)
button_clear = Button(window, text="Clear", padx=79, pady=20, command=lambda: clearing())

button_division = Button(window, text="÷", padx=40, pady=20, command=divisioning)
button_multiplie = Button(window, text="x", padx=39, pady=20, command=zarb)
button_minus = Button(window, text="-", padx=39, pady=20, command=menha)


#Put buttons on the screen
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)

button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)

button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)

button_0.grid(row=4, column=0)

button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)

button_division.grid(row=6, column=0)
button_multiplie.grid(row=6, column=1)
button_minus.grid(row=6, column=2)


window.mainloop()

在波斯语中,“menha”表示减号 在波斯语中,“Zarb”表示乘法 在波斯语中,“taqsim”表示除法

标签: pythontkinter

解决方案


因此,您只需将其添加到您的输出中。我建议使用format()这样做,但您可以使用任何您想要的字符串格式样式。例如:

def calculate():
    if(math == "ezafe"):
        second_number = e.get()
        e.delete(0, END)
        template = "{} + {} = {}"
        result = f_num + int(second_number)
        e.insert(0, template.format(f_num, second_number, result))

推荐阅读