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

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

0

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

Admin آسان 12/ دانلود 732 بازدید

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

3 جواب

نمیتونم این تمرین رو حل کنم!
4
+1
+1
+1
from tkinter import *

def click_javab(labl):
    labl.config(text= "")
    labl.update()
    
def Add(labl, text):
    g = labl['text']
    labl.config(text = g + text)
    
    
def hesab(lable):
    g = lable['text']
    g = g.replace("x", "*")
    javab = eval(g)
    lable.config(text = str(javab))
    lable.update()
    
    
m = Tk()
m.geometry("400x400")
m.title("برنامه ماشین حساب")


javab = Label(m,text="", bd=10)


bt_j = Button(m, text="+", padx=40, pady=20, bd=5, command=lambda:Add(javab, "+"))
bt_m = Button(m, text="-", padx=40, pady=20, bd=5, command=lambda:Add(javab, "-"))
bt_z = Button(m, text="*", padx=40, pady=20, bd=5, command=lambda:Add(javab, "*"))
bt_t = Button(m, text="/", padx=40, pady=20, bd=5, command=lambda:Add(javab, "/"))



b1 = Button(m, text=1, padx=40, pady=20, bd=5, command=lambda:Add(javab,"1"))
b2 = Button(m, text=2, padx=40, pady=20, bd=5, command=lambda:Add(javab,"2"))
b3 = Button(m, text=3, padx=40, pady=20, bd=5, command=lambda:Add(javab,"3"))
b4 = Button(m, text=4, padx=40, pady=20, bd=5, command=lambda:Add(javab,"4"))
b5 = Button(m, text=5, padx=40, pady=20, bd=5, command=lambda:Add(javab,"5"))
b6 = Button(m, text=6, padx=40, pady=20, bd=5, command=lambda:Add(javab,"6"))
b7 = Button(m, text=7, padx=40, pady=20, bd=5, command=lambda:Add(javab, "7"))
b8 = Button(m, text=8, padx=40, pady=20, bd=5, command=lambda:Add(javab,"8"))
b9 = Button(m, text=9, padx=40, pady=20, bd=5, command=lambda:Add(javab,"9"))
b0 = Button(m, text=0, padx=142, pady=20, bd=5, command=lambda:Add(javab,"0"))
b_op = Button(m, text="=", padx=40, pady=100, bd=5, command= lambda:hesab(javab))
btn = Button(m, text="C", padx=40, pady=20, bd=5,command= lambda:click_javab(javab))


bt_j.grid(row=1, column=1)
bt_m.grid(row=1, column=2)
bt_z.grid(row=1, column=3)
bt_t.grid(row=1, column=4)
b1.grid(row=2, column=1)
b2.grid(row=2, column=2)
b3.grid(row=2, column=3)
b4.grid(row=3, column=1)
b5.grid(row=3, column=2)
b6.grid(row=3, column=3)
b7.grid(row=4, column=1)
b8.grid(row=4, column=2)
b9.grid(row=4, column=3)
b0.grid(row=5,columnspan=5, column=2)
b_op.grid(row=2,rowspan=3, column=4)
javab .grid(row=0, column=2)
btn .grid(row=5, column=1)  
m.mainloop()

Reza01 دانلود Python
1
+1
from tkinter import *

#Create a calculator class
class Calculator:

    def __init__(self, master):

        '''
        DOCSTRING: Define what to do on initialization
        '''
        
        #Assign reference to the main window of the application
        self.master = master

        #Add a name to our application
        master.title("Python Calculator")
        
        #Create a line where we display the equation
        self.equation=Entry(master, width=36, borderwidth=5)

        #Assign a position for the equation line in the grey application window
        self.equation.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

        #Execute the .creteButton() method
        self.createButton()


    def createButton(self):
        '''
        DOCSTRING: Method that creates the buttons
        INPUT: nothing
        OUTPUT: creates a button
        '''
        
        #We first create each button one by one with the value we want
        #Using addButton() method which is described below
        b0 = self.addButton(0)
        b1 = self.addButton(1)
        b2 = self.addButton(2)
        b3 = self.addButton(3)
        b4 = self.addButton(4)
        b5 = self.addButton(5)
        b6 = self.addButton(6)
        b7 = self.addButton(7)
        b8 = self.addButton(8)
        b9 =  self.addButton(9)
        b_add = self.addButton('+')
        b_sub = self.addButton('-')
        b_mult = self.addButton('*')
        b_div = self.addButton('/')
        b_clear = self.addButton('c')
        b_equal = self.addButton('=')

        #Arrange the buttons into lists which represent calculator rows
        row1=[b7,b8,b9,b_add]
        row2=[b4,b5,b6,b_sub]
        row3=[b1,b2,b3,b_mult]
        row4=[b_clear,b0,b_equal,b_div]

        #Assign each button to a particular location on the GUI
        r=1
        for row in [row1, row2, row3, row4]:
            c=0
            for buttn in row:
                buttn.grid(row=r, column=c, columnspan=1)
                c+=1
            r+=1


    def addButton(self,value):
            '''
            DOCSTRING: Method to process the creation of a button and make it clickable
            INPUT: value of the button (1,2,3,4,5,6,7,8,9,0,+,-,*,/,c,=)
            OUTPUT: returns a designed button object
            '''
            return Button(self.master, text=value, width=9, command = lambda: self.clickButton(str(value)))


    def clickButton(self, value):
        
        '''
        DOCSTRING: Method to program the actions that will happen in the calculator after a click of each button
        INPUT: value of the button (1,2,3,4,5,6,7,8,9,0,+,-,*,/,c,=)
        OUTPUT: what action will be performed when a particular button is clicked
        '''
        
        #Get the equation that's entered by the user
        current_equation=str(self.equation.get())
        
        #If user clicked "c", then clear the screen
        if value == 'c':
            self.equation.delete(-1, END)
        
        #If user clicked "=", then compute the answer and display it
        elif value == '=':
            answer = str(eval(current_equation))
            self.equation.delete(-1, END)
            self.equation.insert(0, answer)
        
        #If user clicked any other button, then add it to the equation line
        else:
            self.equation.delete(0, END)
            self.equation.insert(-1, current_equation+value)


#Execution
if __name__=='__main__':
    
    #Create the main window of an application
    root = Tk()
    
    #Tell our calculator class to use this window
    my_gui = Calculator(root)
    
    #Executable loop on the application, waits for user input
    root.mainloop( )
Abtin67 دانلود Python
1
+1
import tkinter as tk


def calculate(operation):
    try:
        num1 = float(num1_var.get())
        num2 = float(num2_var.get())
        if operation == 'جمع':
            result = num1 + num2
        elif operation == 'تفریق':
            result = num1 - num2
        elif operation == 'تقسیم':
            if num2 == 0:
                result = 'خطای تقسیم بر صفر'
            else:
                result = num1 / num2
        elif operation == 'ضرب':
            result = num1 * num2
        result_label.config(text='نتیجه: ' + str(result))
    except ValueError:
        result_label.config(text='ورودی نامعتبر')


window = tk.Tk()
window.title('ماشین حساب')
window.geometry("700x500")


# عدد اول
num1_label = tk.Label(window, text='عدد اول:')
num1_label.grid(row=0, column=0)
num1_var = tk.StringVar()
num1_entry = tk.Entry(window, textvariable=num1_var)
num1_entry.grid(row=0, column=1)


# عدد دوم
num2_label = tk.Label(window, text='عدد دوم: ')
num2_label.grid(row=1, column=0)
num2_var = tk.StringVar()
num2_entry = tk.Entry(window, textvariable=num2_var)
num2_entry.grid(row=1, column=1)


# دکمه‌های عملیات
operations = ['جمع', 'تفریق', 'ضرب', 'تقسیم']
for i, op in enumerate(operations):
    button = tk.Button(window, text=op, command=lambda operation=op: calculate(operation))
    button.grid(row=2, column=i)


# نمایش نتایج
result_label = tk.Label(window, text='')
result_label.grid(row=3, columnspan=4)


window.mainloop()

Knowledgebiome دانلود Python

ارسال جواب

// کداتو توی این بخش بنویس
// فرقی نمیکنه چه زبان برنامه نویسی باشه، همرو پشتیبانی میکنیم :)
// البته قبلش این سه خط رو پاک کن


  • تو جوابت میتونی از تصویر، کد، لینک به سایر صفحات و... استفاده کنی
  • لطفا جواب های تکراری ارسال نکن
  • جواب های ارسالی، پس از بررسی کوتاهی، ویرایش میشن و در سایت نمایش داده میشن
  • ارسال جواب حق مادی یا معنوی برای ارسال کننده ایجاد نمیکند و تمام حقوق برای سایت کدبزن محفوظ است

تمرینات مرتبط

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