تمرین برنامه نویسی؛ با کدبزن!

منبع جامع سوالات و تمرینات برنامه نویسی

ماشین حساب گرافیکی

تمرین آسان 12/ دانلود 1184 بازدید

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

6 جواب

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()
Reeeezaprogram دانلود Python
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()
Rezanajafianez دانلود Python
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 دانلود 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 دانلود 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("خطا", "تقسیم بر صفر امکان‌پذیر نیست.")

# ایجاد پنجره اصلی
root = tk.Tk()
root.title("ماشین حساب ساده")

# ایجاد فیلدهای ورودی
entry1 = tk.Entry(root)
entry1.pack(pady=10)

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

# ایجاد دکمه‌ها
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_result = tk.Label(root, text="نتیجه: ")
label_result.pack(pady=20)

# اجرای حلقه اصلی
root.mainloop()
Mma123 دانلود 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))

# ایجاد پنجره اصلی
root = tk.Tk()
root.title("محاسبات عددی")

# فیلدهای ورودی
entry1 = tk.Entry(root)
entry1.pack(pady=10)

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

# دکمه‌ها
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)

# برچسب برای نمایش نتیجه
result_label = tk.Label(root, text="نتیجه: ")
result_label.pack(pady=10)

# اجرای حلقه اصلی
root.mainloop()
User 136 دانلود Python

ارسال جواب

  • قبل از ارسال جواب ویدیو زیر رو ببین تا کار کردن با markdown رو یاد بگیری
  • لطفا جواب های تکراری ارسال نکن
  • سعی کن داخل کدت از کلمات فارسی یا فینگلیش (فارسی با حروف انگلیسی) استفاده نکنی و کدت تماما انگلیسی باشه
  • لطفا داخل جواب از ایموجی یا کاراکترهای خاص استفاده نکن
  • ارسال جواب حق مادی یا معنوی برای ارسال کننده ایجاد نمیکند و تمام حقوق برای سایت کدبزن محفوظ است

راهنمای ارسال جواب 👇

مطالب مرتبط

تشخیص با استفاده از هوش مصنوعی
×
×
بستن