from tkinter import *

# الشاشة الرئسية 
window = Tk()
window.title("جمع الارقام")
window.geometry("245x400")
window.resizable(False,False) # منع توسيع الشاشة 
window.configure(bg="black") # لون 

# Label  عرض شاشة 
myLabel = Label(window, width=33, height=4, bg="#c9bfbf" )
myLabel.grid(row=0 , column=0,columnspan=6,pady=5)

show_num = [] # اضهار الرقم من الشاشة 
sum = "" # حفظ الارقام لجمعها
total = 0  # ناتج النهائي

# اظهار الرقم في شاشة (lable)
def show(num):
    global sum

    show_num.append(num)
    myLabel.config(text=show_num)
    sum += num
    
# خذف الارقام من الشاشة (C)
def clear():  
    global total, sum

    show_num.clear()
    myLabel.config(text=show_num )
    total = 0 
    sum = ""

# جمع الاعداد (=)
def result():
    global total

    sum_list = list(sum.split("+")) 
    # التقاط الاخطاء
    try:
        # في حال المستخد لم يدخل الرقم 
        if sum == "":
            myLabel.config(text="0")
        # حسبة الارقام 
        for number in sum_list:
            total += int(number)
        myLabel.config(text=total)

    except:
        myLabel.config(text="خطأ ,الرجاء محاولة مرة اخرى")
        
    total = 0 # في حال استكمل المستخدم 

# Button اختيار الشخص الرقم 
myButton = Button(window,text="9", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("9"))
myButton.grid(row=1, column=3)
myButton = Button(window,text="8", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("8"))
myButton.grid(row=1, column=2)
myButton = Button(window,text="7", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("7"))
myButton.grid(row=1, column=1)

myButton = Button(window,text="6", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("6"))
myButton.grid(row=2, column=3)
myButton = Button(window,text="5", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("5"))
myButton.grid(row=2, column=2)
myButton = Button(window,text="4", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("4"))
myButton.grid(row=2, column=1)

myButton = Button(window,text="3", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("3"))
myButton.grid(row=3, column=3)
myButton = Button(window,text="2", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("2"))
myButton.grid(row=3, column=2)
myButton = Button(window,text="1", width=8, height=5,bg="#232423",fg="#f5faf5",font="Modern 10 bold",command=lambda: show("1"))
myButton.grid(row=3, column=1)

myButton = Button(window,text="0", width=24, height=4,bg="#232423",fg="#f5faf5",font="Chiller 9 bold",command=lambda: show("0"))
myButton.grid(row=4, column=1,columnspan=3)

myButton = Button(window,text="C", width=8, height=5,bg="#c9bfbf",fg="black",font="Chiller 9 bold", command=lambda: clear())
myButton.grid(row=1, column=4)
myButton = Button(window,text="+", width=8, height=5,bg="#d1ac73",fg="black",font="Chiller 9 bold", command=lambda: show("+"))
myButton.grid(row=2, column=4)
myButton = Button(window,text="=", width=8, height=10,bg="#d98404",fg="black",font="Chiller 9 bold",command=result)
myButton.grid(row=3, column=4,rowspan=2)

window.mainloop()

'''
###################مشروع جمع الارقام بدون (tkinter)###########

number = input("Enter a series pf numbers separated by spaces:").split()
total = 0
try: 
    for num in number:
        total += int(num)
    print(f"The sum of the numbers is:{total}")   
except:
    print("Error, try agin!")

'''
