CodeSolved

Solved Programming Questions & Exercises

Graphic calculator

Practice Easy 12/ Download 1376 Views

Write a graphics program that has 2 fields to enter the number and 4 buttons for subtraction, multiplication and division. By selecting each button, the relevant calculations should be done on the 2 numbers entered and the result is displayed.

7 Answers

import tk
import customtk as ck
window = tk.Tk()
strvar = tk.StringVar()
def set_num(number):
    strvar.set(strvar.get()+number)
def clear_num():
    strvar.set("")
def calnumber():
    javab = str(eval(strvar.get()))
    strvar.set(javab)
def menu():
    window = tk.Tk()
    tk.Label(window, text="ساخته شده توسط آقا رضا", fg="black").place(x=50, y=100, width=150, height=31)
    hj = 2
    def clooose():
        while hj == 2:
            break
    window.title("about us")
    tk.Button(window, text="باشه", fg="black", command=clooose).place(x=90, y=130)
    window.minsize(260, 260)
    window.maxsize(260, 260)
    window.mainloop()
tk.Canvas(window, bg="white").place(x=0, y=0, height=800, width=600)
tk.Canvas(window,border=0, background="#272121").place(x=0, y=0, width=500, height=600)
tk.Button(window, text="0", border=0, foreground="white", background="#443737", command=lambda:set_num("0")).place(x=4, y=297, height=50, width=136)
tk.Button(window, text=".", border=0, foreground="white",background="#443737", command=lambda:set_num(".")).place(x=142, y=297, height=50, width=67)
tk.Button(window, text="1", border=0, foreground="white", bg="#443737", command=lambda:set_num("1")).place(x=4, y=244, height=50, width=67)
tk.Button(window, text="2", border=0, foreground="white", bg="#443737", command=lambda:set_num("2")).place(x=73, y=244, height=50, width=67)
tk.Button(window, text="3", border=0, foreground="white", bg="#443737", command=lambda:set_num("3")).place(x=142, y=244, height=50, width=67)
tk.Button(window, text="4", border=0, foreground="white", bg="#443737", command=lambda:set_num("4")).place(x=4, y=191, height=50, width=67)
tk.Button(window, text="5", border=0, foreground="white", bg="#443737", command=lambda:set_num("5")).place(x=73, y=191, height=50, width=67)
tk.Button(window, text="6", border=0, foreground="white", bg="#443737", command=lambda:set_num("6")).place(x=142, y=191, height=50, width=67)
tk.Button(window, text="7", border=0, foreground="white", bg="#443737", command=lambda:set_num("7")).place(x=4, y=139, height=50, width=67)
tk.Button(window, text="8", border=0, foreground="white", bg="#443737", command=lambda:set_num("8")).place(x=73, y=139, height=50, width=67)
tk.Button(window, text="9", border=0, foreground="white", bg="#443737", command=lambda:set_num("9")).place(x=142, y=139, height=50, width=67)
tk.Button(window, text="=", border=0,command=calnumber, font=("arial", (20)), foreground="black", bg="#E3651D").place(x=211, y=297, height=50, width=62)
tk.Button(window, text="+", border=0, font=("arial", (20)), foreground="green", bg="#233737", command=lambda:set_num("+")).place(x=211, y=244, height=50, width=62)
tk.Button(window, text="-", border=0, font=("arial", (20)), foreground="red", bg="#233737", command=lambda:set_num("-")).place(x=211, y=191, height=50, width=62)
tk.Button(window, text="×", border=0, font=("arial", (20)), foreground="yellow", bg="#233737", command=lambda:set_num("*")).place(x=211, y=139, height=50, width=62)
tk.Entry(window, border=0,textvariable=strvar, bg="#272121", fg="white", font=("tahoma", (30)) ).place(x=15, y=50, width=240, height=50)
threeline = tk.PhotoImage(file="3line.png")
tk.Button(window, image=threeline,command=menu, border=0, bg="#272121").place(x=5, y=5, width=40, height=40)
tk.Button(window, text="c", fg="white", border=0, bg="#272121", command=clear_num).place(x=5, y=98, width=40, height=40)
tavan = tk.PhotoImage(file="X-TN2.PNG")
tk.Button(window, image=tavan, border=0, bg="#272121",fg="white", command=lambda: set_num("**2")).place(x=130, y=98, width=40, height=40)
darsad = tk.PhotoImage("Darsad.png")
tk.Button(window, text="%", border=0, bg="#272121",fg="white", command=lambda: set_num("/100")).place(x=90, y=98, width=40, height=40)
tk.Button(window, text="÷", border=0, bg="#272121",fg="white", command=lambda:set_num("/")).place(x=50, y=98, width=40, height=40)
tk.Label(window, text="calculator",bg="#272121", fg="white", font=("tahoma", (15))).place(x=160, y=10)
window.title("calculator")
window.minsize(270,350)
window.maxsize(270,350)
window.mainloop()
import tkinter
from tkinter import messagebox 

def jame():
    messagebox.showinfo("Hasel :", float(entry1.get()) + float(entry2.get()))
def tafrigh():
    messagebox.showinfo("Hasel :", float(entry1.get()) - float(entry2.get()))
def zarb():
    messagebox.showinfo("Hasel :", float(entry1.get()) * float(entry2.get()))
def taghsim():
    messagebox.showinfo("Hasel :", float(entry1.get()) / float(entry2.get()))

window = tkinter.Tk()
window.title("Mashin Hesab")
window.geometry("500x500")

label = tkinter.Label(text= "2 Adad Vared kon ")
label.pack(pady=10)

entry1 = tkinter.Entry(window)
entry1.pack(pady=10)
entry2 = tkinter.Entry(window)
entry2.pack(pady=5)

button1 = tkinter.Button(
    text = "+",
    width = 1,
    height = 1,
    bg = "black",
    fg = "white",
    command = jame
)
button1.pack(pady=10)

button2 = tkinter.Button(
    text = "-",
    width = 1,
    height = 1,
    bg = "black",
    fg = "white",
    command = tafrigh
)
button2.pack(pady=10)

button3 = tkinter.Button(
    text = "x",
    width = 1,
    height = 1,
    bg = "black",
    fg = "white",
    command = zarb
)
button3.pack(pady=10)

button4 = tkinter.Button(
    text = "/",
    width = 1,
    height = 1,
    bg = "black",
    fg = "white",
    command = taghsim
)
button4.pack(pady=10)

window.mainloop()
from tkinter import *
root = Tk()
root.title("simple calculator")
e = Entry(root, width=50, borderwidth=10)
e.grid(row=0, column=0, columnspan=4)
f_number = None 
operator = None  

def add():
    global f_number  
    global operator  
    operator = '+'  
    f_number = int(e.get())  
    e.delete(0, END)  

def Division():
    global f_number
    global operator
    operator = '/'
    f_number = int(e.get())
    e.delete(0, END)

def minus():
    global f_number
    global operator
    operator = '-'
    f_number = int(e.get())
    e.delete(0, END)

def Multiplication():
    global f_number
    global operator
    operator = '*'
    f_number = int(e.get())
    e.delete(0, END)

def Remainder():
    global f_number
    global operator
    operator = '%'
    f_number = int(e.get())
    e.delete(0, END)

def power():
    global f_number
    global operator
    operator = '**'
    f_number = int(e.get())
    e.delete(0, END)

def button_click(num: int):
    c = e.get()
    e.delete(0, END)
    e.insert(0, str(c) + str(num)) 

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

def button_equal():
    second_number = e.get()  
    e.delete(0, END)  

    if operator == '+':  # conditions
        e.insert(0, f_number + int(second_number)) 
    elif operator == '-':
        e.insert(0, f_number - int(second_number))  
    elif operator == '*':
        e.insert(0, f_number * int(second_number))  
    elif operator == '**':
        e.insert(0, f_number ** int(second_number))  
    elif operator == '/':
        e.insert(0, f_number / int(second_number))  
    elif operator == '%':
        e.insert(0, f_number % int(second_number))  

button_1 = Button(root, text='1', padx=40, pady=20,
                  command=lambda: button_click(1))
button_2 = Button(root, text='2', padx=40, pady=20,
                  command=lambda: button_click(2))
button_3 = Button(root, text='3', padx=40, pady=20,
                  command=lambda: button_click(3))
button_4 = Button(root, text='4', padx=40, pady=20,
                  command=lambda: button_click(4))
button_5 = Button(root, text='5', padx=40, pady=20,
                  command=lambda: button_click(5))
button_6 = Button(root, text='6', padx=40, pady=20,
                  command=lambda: button_click(6))
button_7 = Button(root, text='7', padx=40, pady=20,
                  command=lambda: button_click(7))
button_8 = Button(root, text='8', padx=40, pady=20,
                  command=lambda: button_click(8))
button_9 = Button(root, text='9', padx=40, pady=20,
                  command=lambda: button_click(9))
button_0 = Button(root, text='0', padx=40, pady=20,
                  command=lambda: button_click(0))
button_clear1 = Button(root, text='clear', padx=30,
                       pady=20, command=button_clear)
button_equal1 = Button(root, text='=', padx=39,
                       pady=20, command=button_equal)
button_add1 = Button(root, text='+', padx=38,
                     pady=20, command=add)
button_Division = Button(root, text='/', padx=40,
                         pady=20, command=Division)
button_minus = Button(root, text='-', padx=39,
                      pady=20, command=minus)
button_Multiplication = Button(root, text='*',
                               padx=40, pady=20, command=Multiplication)
button_Remainder = Button(root, text='%', padx=38,
                          pady=20, command=Remainder)
button_power = Button(root, text='**', padx=38,
                      pady=20, command=power)

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_clear1.grid(row=4, column=1)
button_equal1.grid(row=4, column=2)
button_add1.grid(row=5, column=0)
button_Division.grid(row=5, column=1)
button_Multiplication.grid(row=5, column=2)
button_minus.grid(row=6, column=0)
button_Remainder.grid(row=6, column=1)
button_power.grid(row=6, column=2)
root.mainloop()

User 720 Download Python
import tkinter as tk
def sum():
    try:
        input1 =int(entry1.get())
    except:
        input1=0
    try:
        input2 =int(entry2.get())
    except:
        input2=0
    label.config(text=f"sum: {input1+input2}")

def minus():
    try:
        input1 =int(entry1.get())
    except:
        input1=0
    try:
        input2 =int(entry2.get())
    except:
        input2=0
    label.config(text=f"minus: {input1-input2}")

def divide():
    try:
        input1 =int(entry1.get())
    except:
        input1=0
    try:
        input2 =int(entry2.get())
    except:
        input2=0
    if input2==0:
        label.config(text=f"divide: not divide to zero")
    else:
        label.config(text=f"divide: {input1/input2}")

def multiple():
    try:
        input1 =int(entry1.get())
    except:
        input1=0
    try:
        input2 =int(entry2.get())
    except:
        input2=0
    label.config(text=f"multiple: {input1*input2}")

def main():
    global entry1, entry2, label
    root = tk.Tk()
    root.geometry("400x400")
    root.title("Simple Calculator")

    tk.Label(root, text="Num 1:").pack()
    entry1 = tk.Entry(root, width=5)
    entry1.pack(pady=5)

    tk.Label(root, text="Num 2:").pack()
    entry2 = tk.Entry(root, width=5)
    entry2.pack(pady=5)

    btn_sum = tk.Button(root, text=" + ", command=sum)
    btn_sum.pack(pady=10)
    btn_minus = tk.Button(root, text=" - ", command=minus)
    btn_minus.pack(pady=10)
    btn_divide = tk.Button(root, text=" / ", command=divide)
    btn_divide.pack(pady=10)
    btn_multiple = tk.Button(root, text=" * ", command=multiple)
    btn_multiple.pack(pady=10) 
    label = tk.Label(root, text="")
    label.pack(pady=10)

    root.mainloop()

if __name__ == "__main__":
    main()
Saeeda33 Download Python
from tkinter import *
window = Tk()
window.title("caculater")
window.geometry("215x199")
window.config(background="white")
# window.iconphoto(True,PhotoImage(data=icon))
# window.iconbitmap(icon)
def click(num):
    display.insert(END,num)
def click(amal):
    display.insert(END,amal)
def equal():
    e = display.get()
    result = eval(e)
    display.delete(0,END)
    display.insert(END,result)
def clear():
    display.delete(0,END)
display = Entry(window,border=20,font=("Arial",12) , justify="left",background="black",foreground="white")
btn1 = Button(window,text="1", font=("Arial",12),bg="red" ,command=lambda  c =1:click(c))
btn2 = Button(window, text="2",font=("Arial",12),bg="red",command=lambda  c =2:click(c))
btn3 = Button(window, text="3",font=("Arial",12),bg="red",command=lambda  c =3:click(c))
btn4 = Button(window, text="4",font=("Arial",12),bg="pink",command=lambda  c =4:click(c))
btn5 = Button(window, text="5",font=("Arial",12),bg="pink",command=lambda  c =5:click(c))
btn6 = Button(window,text="6",font=("Arial",12),bg="pink",command=lambda  c =6:click(c))
btn7 = Button(window, text="7",font=("Arial",12),bg="green",command=lambda  c =7:click(c))
btn8 =Button(window,text="8",font=("Arial",12),bg="green",command=lambda  c =8:click(c))
btn9 = Button(window,text="9",font=("Arial",12) , bg="green",command=lambda  c =9:click(c))
btn0 = Button(window, text="0", font=("Arial",12),bg="blue",command=lambda  c =0:click(c))
btnsum = Button(window, text="+",font=("Arial",12) , bg="red" , command=lambda o = "+":click(o))
btntafrigh = Button(window, text="-", font=("Arial",12) , bg="pink" , command=lambda o = "-":click(o))
btnzarb = Button(window, text="*",font=("Arial",12) , bg="green" , command= lambda o = "*":click(o))
btntagh = Button(window, text="/",font=("Arial",12) , bg="blue", command= lambda o = "/":click(o))
btnC =Button(window, text="C",bg="blue",font=("Arial",12),command=clear)
btnmo = Button(window,text="=",font=("Arial",12),bg="blue",command=equal)

display.grid(row=0,column=0,sticky="news",columnspan=4)
btn7.grid(row=2,column=0 ,sticky="news",columnspan=1)
btn8.grid(row=2,column=1,sticky="news",columnspan=1)
btn9.grid(row=2,column=2 ,sticky="news",columnspan=1)
btnzarb.grid(row=2,column=3,sticky="news",columnspan=1)
btn4.grid(row=3,column=0,sticky="news",columnspan=1)
btn5.grid(row=3,column=1,sticky="news",columnspan=1)
btn6.grid(row=3,column=2,sticky="news",columnspan=1)
btntafrigh.grid(row=3,column=3,sticky="news",columnspan=1)
btn1.grid(row=4,column=0,sticky="news",columnspan=1)
btn2.grid(row=4,column=1,sticky="news",columnspan=1)
btn3.grid(row=4,column=2,sticky="news",columnspan=1)
btnsum.grid(row=4,column=3,sticky="news",columnspan=1)
btnC.grid(row=5,column=0,sticky="news",columnspan=1)
btn0.grid(row=5,column=1,sticky="news",columnspan=1)
btntagh.grid(row=5,column=2,sticky="news",columnspan=1)
btnmo.grid(row=5,column=3,sticky="news",columnspan=1)

window.mainloop()
Sumy.amiri Download Python
import tkinter as tk
from tkinter import messagebox

def add():
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        result = num1 + num2
        label_result.config(text=f"نتیجه: {result}")
    except ValueError:
        messagebox.showerror("خطا", "لطفاً اعداد صحیح وارد کنید.")

def subtract():
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        result = num1 - num2
        label_result.config(text=f"نتیجه: {result}")
    except ValueError:
        messagebox.showerror("خطا", "لطفاً اعداد صحیح وارد کنید.")

def multiply():
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        result = num1 * num2
        label_result.config(text=f"نتیجه: {result}")
    except ValueError:
        messagebox.showerror("خطا", "لطفاً اعداد صحیح وارد کنید.")

def divide():
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        if num2 == 0:
            raise ZeroDivisionError
        result = num1 / num2
        label_result.config(text=f"نتیجه: {result}")
    except ValueError:
        messagebox.showerror("خطا", "لطفاً اعداد صحیح وارد کنید.")
    except ZeroDivisionError:
        messagebox.showerror("خطا", "تقسیم بر صفر امکان‌پذیر نیست.")

# Create the main window
root = tk.Tk()
root.title("ماشین حساب ساده")

# Creating input fields
entry1 = tk.Entry(root)
entry1.pack(pady=10)

entry2 = tk.Entry(root)
entry2.pack(pady=10)

# Creating buttons
btn_add = tk.Button(root, text="جمع", command=add)
btn_add.pack(pady=5)

btn_subtract = tk.Button(root, text="تفریق", command=subtract)
btn_subtract.pack(pady=5)

btn_multiply = tk.Button(root, text="ضرب", command=multiply)
btn_multiply.pack(pady=5)

btn_divide = tk.Button(root, text="تقسیم", command=divide)
btn_divide.pack(pady=5)

# Label to display the result
label_result = tk.Label(root, text="نتیجه: ")
label_result.pack(pady=20)

# Implementation of the main loop
root.mainloop()
Mma123 Download Python
import tkinter as tk
from tkinter import messagebox

def calculate(operation):
    try:
        num1 = float(entry1.get())
        num2 = float(entry2.get())
        
        if operation == 'add':
            result = num1 + num2
        elif operation == 'subtract':
            result = num1 - num2
        elif operation == 'multiply':
            result = num1 * num2
        elif operation == 'divide':
            if num2 == 0:
                raise ValueError("Division by zero is not allowed.")
            result = num1 / num2
        
        result_label.config(text=f"نتیجه: {result}")
    except ValueError as e:
        messagebox.showerror("خطا", str(e))

# Create the main window
root = tk.Tk()
root.title("محاسبات عددی")

# Input fields
entry1 = tk.Entry(root)
entry1.pack(pady=10)

entry2 = tk.Entry(root)
entry2.pack(pady=10)

# Buttons
button_add = tk.Button(root, text="جمع", command=lambda: calculate('add'))
button_add.pack(pady=5)

button_subtract = tk.Button(root, text="تفریق", command=lambda: calculate('subtract'))
button_subtract.pack(pady=5)

button_multiply = tk.Button(root, text="ضرب", command=lambda: calculate('multiply'))
button_multiply.pack(pady=5)

button_divide = tk.Button(root, text="تقسیم", command=lambda: calculate('divide'))
button_divide.pack(pady=5)

# Label to display the result
result_label = tk.Label(root, text="نتیجه: ")
result_label.pack(pady=10)

# Implementation of the main loop
root.mainloop()
User 136 Download Python

Submit answer

Submitting answers is currently unavailable.

Related content

Detection using AI
×
×
Close