• 1402/07/05

پروژه درست کردن ماشین حساب با تعریف دکمه اعداد در محیط نرم‌افزار :

سلام استاد خسته نباشید 

من سعی کردم با استفاده از درست کردن یک کلاس، اون ماشین حساب رو درست کنم ، اما هنگامی که کد رو اجرا می کنم، همه چی خوبه تا وقتی که فقط عدد وارد می کنم، همین که یک بعلاوه یا یکی دیگر از اون سه عمل رو وارد می کنم به ارور بر می خورم...

مشکل از کجاست و چطور حلش کنم؟ 

ممنون می‌شم پاسخ بدین ...

کد ها  و ارور و فضای نرم افزار :

import tkinter as tk

# =========================== General settings ===========================
class calculator_class(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Calculator")
        self.geometry("470x490")
        self.resizable(width= False, height=False)
        self.res = tk.Entry(self, font= ("Arial",30), bg= "gray")
        self.res.grid(row= 0, column= 0, columnspan= 4, padx= 40, pady= 20, ipadx= 20
                      ,ipady= 8, sticky= tk.E + tk.W)
        self.res.config(justify= tk.LEFT)
        self.grid_columnconfigure(0, weight=1)

        button_frame = tk.Frame(self)
        button_frame.grid(row= 1 , column = 0, padx= 10, pady= 10)

# =========================== buttons ===========================

        self.num_and_op_btn("1", button_frame, 0,0, lambda : self.add_number(1))
        self.num_and_op_btn("2", button_frame, 0,1, lambda : self.add_number(2))
        self.num_and_op_btn("3", button_frame, 0,2, lambda : self.add_number(3))
        self.num_and_op_btn("4", button_frame, 1,0, lambda : self.add_number(4))
        self.num_and_op_btn("5", button_frame, 1,1, lambda : self.add_number(5))
        self.num_and_op_btn("6", button_frame, 1,2, lambda : self.add_number(6))
        self.num_and_op_btn("7", button_frame, 2,0, lambda : self.add_number(7))
        self.num_and_op_btn("8", button_frame, 2,1, lambda : self.add_number(8))
        self.num_and_op_btn("9", button_frame, 2,2, lambda : self.add_number(9))
        self.num_and_op_btn("0", button_frame, 3,1, lambda : self.add_number(0))

        self.num_and_op_btn("+", button_frame, 0,3, lambda : self.add_opp("+"))
        self.num_and_op_btn("-", button_frame, 1,3, lambda : self.add_opp("-"))
        self.num_and_op_btn("*", button_frame, 2,3, lambda : self.add_opp("*"))
        self.num_and_op_btn("/", button_frame, 3,3, lambda : self.add_opp("/"))

        self.num_and_op_btn("=", button_frame, 3,2, self.calculate , bg= "green")
        self.num_and_op_btn("C", button_frame, 3,0, self.clear, bg= "red")
# =========================== functions ===========================
    def num_and_op_btn(self, text, frame, row , column, command, bg= "white"):
        button = tk.Button(frame, text=text, command= command, font= ("Arial",15), width= 3, height= 2 )
        button.config(bg= bg)
        button.grid(row = row, column= column, padx= 10, pady= 10)
    def add_number(self, num):
        current = self.res.get()
        current += str(num)
        self.res.delete(0, len(current))
        self.res.insert(0, eval(current))

    def add_opp(self, opp):
        current = self.res.get()
        current += opp
        self.res.delete(0,len(current))
        self.res.insert(0,eval(current))

    def calculate(self):
        current = self.res.get()
        self.res.delete(0,len(current))
        self.res.insert(0, eval(current))

    def clear(self):
        current = self.res.get()
        self.res.delete(0, len(current))


calc = calculator_class()
calc.mainloop()
logo-samandehi