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

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

0

ماشین حساب حرفه ای

Admin آسان 33/ دانلود 1260 بازدید

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

5 جواب

نمیتونم این تمرین رو حل کنم!
2
+1
+1
import kivy
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

class UI(BoxLayout):
    def __init__(self, **kwargs):
        super(UI, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.inputnumber = TextInput(multiline = False, size_hint_y=0.11)
        self.add_widget(self.inputnumber)
        self.grid()
    def grid(self):
        self.mygrid = GridLayout(cols = 4)
        self.btn7 = Button(text='7', size_hint_y=0.38, on_press=self.calculate)
        self.btn8 = Button(text='8', size_hint_y=0.38, on_press=self.calculate)
        self.btn9 = Button(text='9', size_hint_y=0.38, on_press=self.calculate)
        self.btnplus = Button(text='+', size_hint_y=0.38, on_press=self.calculate)
        self.btn4 = Button(text='4', size_hint_y=0.38, on_press=self.calculate)
        self.btn5 = Button(text='5', size_hint_y=0.38, on_press=self.calculate)
        self.btn6 = Button(text='6', size_hint_y=0.38, on_press=self.calculate)
        self.btnmines = Button(text='-', size_hint_y=0.38, on_press=self.calculate)
        self.btn1 = Button(text='1', size_hint_y=0.38, on_press=self.calculate)
        self.btn2 = Button(text='2', size_hint_y=0.38, on_press=self.calculate)
        self.btn3 = Button(text='3', size_hint_y=0.38, on_press=self.calculate)
        self.btnzarb = Button(text='*', size_hint_y=0.38, on_press=self.calculate)
        self.btnclear = Button(text='AC', size_hint_y=0.38, on_press=self.clear)
        self.btn0 = Button(text='0', size_hint_y=0.38, on_press=self.calculate)
        self.btneval = Button(text='=', size_hint_y=0.38, on_press=self.calculate)
        self.btntagsim = Button(text='/', size_hint_y=0.38, on_press=self.calculate)
        self.mygrid.add_widget(self.btn7)
        self.mygrid.add_widget(self.btn8)
        self.mygrid.add_widget(self.btn9)
        self.mygrid.add_widget(self.btnplus)
        self.mygrid.add_widget(self.btn4)
        self.mygrid.add_widget(self.btn5)
        self.mygrid.add_widget(self.btn6)
        self.mygrid.add_widget(self.btnmines)
        self.mygrid.add_widget(self.btn1)
        self.mygrid.add_widget(self.btn2)
        self.mygrid.add_widget(self.btn3)
        self.mygrid.add_widget(self.btnzarb)
        self.mygrid.add_widget(self.btnclear)
        self.mygrid.add_widget(self.btn0)
        self.mygrid.add_widget(self.btneval)
        self.mygrid.add_widget(self.btntagsim)
        self.add_widget(self.mygrid)

    def calculate(self, instance):
        operator = instance.text
        if operator == '=':
            try:
                result = str(eval(self.inputnumber.text))
            except Exception as e:
                result = 'Error'

            self.inputnumber.text = result
        else:
            self.inputnumber.text += operator

    def clear(self, event):
        self.inputnumber.text = ""
        
class start(App):
    def build(self):
        return UI()

if __name__ == '__main__':
    start().run()          

کاربر 416 دانلود Python
1
+1
import kivy
from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout

class UI(BoxLayout):
    def __init__(self, **kwargs):
        super(UI, self).__init__(**kwargs)
        self.orientation = 'vertical'
        self.inputnumber = TextInput(multiline = False, size_hint_y=0.11)
        self.add_widget(self.inputnumber)
        self.grid()
    def grid(self):
        self.mygrid = GridLayout(cols = 4)
        self.btn7 = Button(text='7', size_hint_y=0.38, on_press=self.calculate)
        self.btn8 = Button(text='8', size_hint_y=0.38, on_press=self.calculate)
        self.btn9 = Button(text='9', size_hint_y=0.38, on_press=self.calculate)
        self.btnplus = Button(text='+', size_hint_y=0.38, on_press=self.calculate)
        self.btn4 = Button(text='4', size_hint_y=0.38, on_press=self.calculate)
        self.btn5 = Button(text='5', size_hint_y=0.38, on_press=self.calculate)
        self.btn6 = Button(text='6', size_hint_y=0.38, on_press=self.calculate)
        self.btnmines = Button(text='-', size_hint_y=0.38, on_press=self.calculate)
        self.btn1 = Button(text='1', size_hint_y=0.38, on_press=self.calculate)
        self.btn2 = Button(text='2', size_hint_y=0.38, on_press=self.calculate)
        self.btn3 = Button(text='3', size_hint_y=0.38, on_press=self.calculate)
        self.btnzarb = Button(text='*', size_hint_y=0.38, on_press=self.calculate)
        self.btnclear = Button(text='AC', size_hint_y=0.38, on_press=self.clear)
        self.btn0 = Button(text='0', size_hint_y=0.38, on_press=self.calculate)
        self.btneval = Button(text='=', size_hint_y=0.38, on_press=self.calculate)
        self.btntagsim = Button(text='/', size_hint_y=0.38, on_press=self.calculate)
        self.mygrid.add_widget(self.btn7)
        self.mygrid.add_widget(self.btn8)
        self.mygrid.add_widget(self.btn9)
        self.mygrid.add_widget(self.btnplus)
        self.mygrid.add_widget(self.btn4)
        self.mygrid.add_widget(self.btn5)
        self.mygrid.add_widget(self.btn6)
        self.mygrid.add_widget(self.btnmines)
        self.mygrid.add_widget(self.btn1)
        self.mygrid.add_widget(self.btn2)
        self.mygrid.add_widget(self.btn3)
        self.mygrid.add_widget(self.btnzarb)
        self.mygrid.add_widget(self.btnclear)
        self.mygrid.add_widget(self.btn0)
        self.mygrid.add_widget(self.btneval)
        self.mygrid.add_widget(self.btntagsim)
        self.add_widget(self.mygrid)

    def calculate(self, instance):
        operator = instance.text
        if operator == '=':
            try:
                result = str(eval(self.inputnumber.text))
            except Exception as e:
                result = 'Error'

            self.inputnumber.text = result
        else:
            self.inputnumber.text += operator

    def clear(self, event):
        self.inputnumber.text = ""
        
class start(App):
    def build(self):
        return UI()

if __name__ == '__main__':
    start().run()         
کاربر 669 دانلود Python Android
0
from tkinter import *

calculation = ""


def add_to_calculation(symbol):
    global calculation
    calculation += str(symbol)
    text_result.delete(1.0, 'end')
    text_result.insert(1.0, calculation)


def evaluate_calculation():
    global calculation
    try:
        calculation = str(eval(calculation))
        text_result.delete(1.0, 'end')
        text_result.insert(1.0, calculation)
    except:
        clear_fields()
        text_result.insert(1.0, "Error")


def clear_fields():
    global calculation
    calculation = ""
    text_result.delete(1.0, 'end')


root = Tk()
root.minsize(300,275)
root.maxsize(300,275)
root.title('calculator')

text_result = Text(root, height=2, width=16, font=('Arial', 24))
text_result.insert(1.0, "hosein_mhg")
text_result.grid(columnspan=5)

btn_1 = Button(root, text='1', command=lambda: add_to_calculation(1), width=5, font=("Arial", 14))
btn_1.grid(row=2, column=1)
btn_2 = Button(root, text='2', command=lambda: add_to_calculation(2), width=5, font=("Arial", 14))
btn_2.grid(row=2, column=2)
btn_3 = Button(root, text='3', command=lambda: add_to_calculation(3), width=5, font=("Arial", 14))
btn_3.grid(row=2, column=3)
btn_4 = Button(root, text='4', command=lambda: add_to_calculation(4), width=5, font=("Arial", 14))
btn_4.grid(row=3, column=1)
btn_5 = Button(root, text='5', command=lambda: add_to_calculation(5), width=5, font=("Arial", 14))
btn_5.grid(row=3, column=2)
btn_6 = Button(root, text='6', command=lambda: add_to_calculation(6), width=5, font=("Arial", 14))
btn_6.grid(row=3, column=3)
btn_7 = Button(root, text='7', command=lambda: add_to_calculation(7), width=5, font=("Arial", 14))
btn_7.grid(row=4, column=1)
btn_8 = Button(root, text='8', command=lambda: add_to_calculation(8), width=5, font=("Arial", 14))
btn_8.grid(row=4, column=2)
btn_9 = Button(root, text='9', command=lambda: add_to_calculation(9), width=5, font=("Arial", 14))
btn_9.grid(row=4, column=3)
btn_0 = Button(root, text='0', command=lambda: add_to_calculation(0), width=5, font=("Arial", 14))
btn_0.grid(row=5, column=2)
btn_plus = Button(root, text='+', command=lambda: add_to_calculation('+'), width=5, font=("Arial", 14))
btn_plus.grid(row=2, column=4)
btn_minus = Button(root, text='-', command=lambda: add_to_calculation('-'), width=5, font=("Arial", 14))
btn_minus.grid(row=3, column=4)
btn_mul = Button(root, text='*', command=lambda: add_to_calculation('*'), width=5, font=("Arial", 14))
btn_mul.grid(row=4, column=4)
btn_div = Button(root, text='/', command=lambda: add_to_calculation('/'), width=5, font=("Arial", 14))
btn_div.grid(row=5, column=4)
btn_open = Button(root, text='(', command=lambda: add_to_calculation('('), width=5, font=("Arial", 14))
btn_open.grid(row=5, column=1)
btn_close = Button(root, text=')', command=lambda: add_to_calculation(')'), width=5, font=("Arial", 14))
btn_close.grid(row=5, column=3)
btn_clear = Button(root, text='C', command=clear_fields, width=11, font=("Arial", 14))
btn_clear.grid(row=6, column=1, columnspan=2)
btn_equals = Button(root, text='=', command=evaluate_calculation, width=11, font=("Arial", 14))
btn_equals.grid(row=6, column=3, columnspan=2)
root.mainloop()
Hosein10 دانلود Python
0
# import everything from tkinter module 
from tkinter import *
 
# globally declare the expression variable 
expression = "" 
 
 
# Function to update expression 
# in the text entry box 
def press(num): 
    # point out the global expression variable 
    global expression 
 
    # concatenation of string 
    expression = expression + str(num) 
 
    # update the expression by using set method 
    equation.set(expression) 
 
 
# Function to evaluate the final expression 
def equalpress(): 
    # Try and except statement is used 
    # for handling the errors like zero 
    # division error etc. 
 
    # Put that code inside the try block 
    # which may generate the error 
    try: 
 
        global expression 
 
        # eval function evaluate the expression 
        # and str function convert the result 
        # into string 
        total = str(eval(expression)) 
 
        equation.set(total) 
 
        # initialize the expression variable 
        # by empty string 
        expression = "" 
 
    # if error is generate then handle 
    # by the except block 
    except: 
 
        equation.set(" error ") 
        expression = "" 
 
 
# Function to clear the contents 
# of text entry box 
def clear(): 
    global expression 
    expression = "" 
    equation.set("") 
 
 
# Driver code 
if __name__ == "__main__": 
    # create a GUI window 
    gui = Tk() 
 
    # set the background colour of GUI window 
    gui.configure(background="light green") 
 
    # set the title of GUI window 
    gui.title("Simple Calculator") 
 
    # set the configuration of GUI window 
    gui.geometry("270x150") 
 
    # StringVar() is the variable class 
    # we create an instance of this class 
    equation = StringVar() 
 
    # create the text entry box for 
    # showing the expression . 
    expression_field = Entry(gui, textvariable=equation) 
 
    # grid method is used for placing 
    # the widgets at respective positions 
    # in table like structure . 
    expression_field.grid(columnspan=4, ipadx=70)
 
    # create a Buttons and place at a particular 
    # location inside the root window . 
    # when user press the button, the command or 
    # function affiliated to that button is executed . 
    button1 = Button(gui, text=' 1 ', fg='black', bg='red', 
                    command=lambda: press(1), height=1, width=7) 
    button1.grid(row=2, column=0) 
 
    button2 = Button(gui, text=' 2 ', fg='black', bg='red', 
                    command=lambda: press(2), height=1, width=7) 
    button2.grid(row=2, column=1) 
 
    button3 = Button(gui, text=' 3 ', fg='black', bg='red', 
                    command=lambda: press(3), height=1, width=7) 
    button3.grid(row=2, column=2) 
 
    button4 = Button(gui, text=' 4 ', fg='black', bg='red', 
                    command=lambda: press(4), height=1, width=7) 
    button4.grid(row=3, column=0) 
 
    button5 = Button(gui, text=' 5 ', fg='black', bg='red', 
                    command=lambda: press(5), height=1, width=7) 
    button5.grid(row=3, column=1) 
 
    button6 = Button(gui, text=' 6 ', fg='black', bg='red', 
                    command=lambda: press(6), height=1, width=7) 
    button6.grid(row=3, column=2) 
 
    button7 = Button(gui, text=' 7 ', fg='black', bg='red', 
                    command=lambda: press(7), height=1, width=7) 
    button7.grid(row=4, column=0) 
 
    button8 = Button(gui, text=' 8 ', fg='black', bg='red', 
                    command=lambda: press(8), height=1, width=7) 
    button8.grid(row=4, column=1) 
 
    button9 = Button(gui, text=' 9 ', fg='black', bg='red', 
                    command=lambda: press(9), height=1, width=7) 
    button9.grid(row=4, column=2) 
 
    button0 = Button(gui, text=' 0 ', fg='black', bg='red', 
                    command=lambda: press(0), height=1, width=7) 
    button0.grid(row=5, column=0) 
 
    plus = Button(gui, text=' + ', fg='black', bg='red', 
                command=lambda: press("+"), height=1, width=7) 
    plus.grid(row=2, column=3) 
 
    minus = Button(gui, text=' - ', fg='black', bg='red', 
                command=lambda: press("-"), height=1, width=7) 
    minus.grid(row=3, column=3) 
 
    multiply = Button(gui, text=' * ', fg='black', bg='red', 
                    command=lambda: press("*"), height=1, width=7) 
    multiply.grid(row=4, column=3) 
 
    divide = Button(gui, text=' / ', fg='black', bg='red', 
                    command=lambda: press("/"), height=1, width=7) 
    divide.grid(row=5, column=3) 
 
    equal = Button(gui, text=' = ', fg='black', bg='red', 
                command=equalpress, height=1, width=7) 
    equal.grid(row=5, column=2) 
 
    clear = Button(gui, text='Clear', fg='black', bg='red', 
                command=clear, height=1, width=7) 
    clear.grid(row=5, column='1') 
 
    Decimal= Button(gui, text='.', fg='black', bg='red', 
                    command=lambda: press('.'), height=1, width=7) 
    Decimal.grid(row=6, column=0) 
    # start the GUI 
    gui.mainloop()
کاربر 1454 دانلود Python
0
from tkinter import *
from tkinter import ttk
#========================main================================
window=Tk()
window.title('ماشین حساب')
window.geometry('450x250')
window.configure(bg='black')
window.resizable(width=False,height=False)
expression=''
expression2=''
selected_input=1
opration=''
expression3=''
#=======================buttons===============================
button_0=ttk.Button(window,text=0,command=lambda:set_number(0))
button_0.place(x=0,y=200,height=45,width=45)
button_1=ttk.Button(window,text=1,command=lambda:set_number(1))
button_1.place(x=45,y=200,height=45,width=45)
button_2=ttk.Button(window,text=2,command=lambda:set_number(2))
button_2.place(x=90,y=200,height=45,width=45)
button_3=ttk.Button(window,text=3,command=lambda:set_number(3))
button_3.place(x=135,y=200,height=45,width=45)
button_4=ttk.Button(window,text=4,command=lambda:set_number(4))
button_4.place(x=180,y=200,height=45,width=45)
button_5=ttk.Button(window,text=5,command=lambda:set_number(5))
button_5.place(x=225,y=200,height=45,width=45)
button_6=ttk.Button(window,text=6,command=lambda:set_number(6))
button_6.place(x=270,y=200,height=45,width=45)
button_7=ttk.Button(window,text=7,command=lambda:set_number(7))
button_7.place(x=315,y=200,height=45,width=45)
button_8=ttk.Button(window,text=8,command=lambda:set_number(8))
button_8.place(x=360,y=200,height=45,width=45)
button_9=ttk.Button(window,text=9,command=lambda:set_number(9))
button_9.place(x=405,y=200,height=45,width=45)
button_plus=ttk.Button(window,text='+',command=lambda :set_opration('+'))
button_plus.place(y=150,height=45,width=45)
button_10=ttk.Button(window,text='-',command=lambda :set_opration('-'))
button_10.place(y=150,x=45,height=45,width=45)
button_11=ttk.Button(window,text='x',command=lambda :set_opration('*'))
button_11.place(y=150,x=90,height=45,width=45)
button_12=ttk.Button(window,text='÷',command=lambda :set_opration('/'))
button_12.place(y=150,x=135,height=45,width=45)
button_13=ttk.Button(window,text='=',command=lambda :show_result())
button_13.place(y=150,x=180,height=45,width=45)
button_14=ttk.Button(window,text='fild1',command=lambda :set_input1())
button_14.place(y=150,x=225,height=45,width=45)
button_15=ttk.Button(window,text='fild2',command=lambda:set_input2())
button_15.place(y=150,x=270,height=45,width=45)
button_16=ttk.Button(window,text='تقسیم صحیح',command=lambda :set_opration('//'))
button_16.place(y=100,x=0,height=45,width=90)
button_17=ttk.Button(window,text='توان',command=lambda :set_opration('**'))
button_17.place(y=100,x=90,height=45,width=45)
button_18=ttk.Button(window,text='%',command=lambda :set_opration('درصد'))
button_18.place(y=100,x=135,height=45,width=45)
button_19=ttk.Button(window,text='باقی مانده ی تقسیم صحیح',command=lambda :set_opration('%'))
button_19.place(y=100,x=180,height=45,width=135)
btn_20=ttk.Button(window,text='بستن',command=window.destroy)
btn_20.place(width=45,height=45,x=315,y=100)
button_21=ttk.Button(window,text='c',command=lambda:cl())
button_21.place(width=45,height=45, y=150,x=315)
#=======================entry===============================
num_1=StringVar()
input_number1= Entry(window,textvariable=num_1)
input_number1.place(x=1,y=1,height=25,width=200)
num_2=StringVar()
input_number2= Entry(window,textvariable=num_2)
input_number2.place(x=249,y=1,height=25,width=200)
num_3=StringVar()
new=Entry(window,textvariable=num_3)
new.place(x=11,y=27,height=25,width=427)
#=====================def================================
def set_number(number):
    global expression
    global expression2
    expression=expression+str(number)
    expression2 = expression2 + str(number)
    if selected_input==1:
        expression2 =''
        num_1.set(expression)
    elif selected_input==2:
        expression=''
        num_2.set(expression2)
def set_input1():
    global selected_input
    selected_input=1
def set_input2():
    global selected_input
    selected_input=2
def set_opration(op):
    global opration
    opration=op
    global selected_input
    selected_input=2
def cl():
    global expression
    global
expression2
    global expression3
    expression=' '
    expression2=' '
    num_1.set(expression)
    num_2.set(expression2)
    num_3.set(expression3)
    global selected_input
    selected_input=1
def show_result():
    global selected_input
    selected_input = 1if opration=='+':
        result=float(num_1.get())+float(num_2.get())
        num_3.set(result)
    elif opration=='-':
        result=float(num_1.get())-float(num_2.get())
        num_3.set(result)
    elif opration=='*':
        result=float(num_1.get())*float(num_2.get())
        num_3.set(result)
    elif opration=='/':
        result=float(num_1.get())/float(num_2.get())
        num_3.set(result)
    elif opration=='//':
        result=float(num_1.get())//float(num_2.get())
        num_3.set(result)
    elif opration=='**':
        result=float(num_1.get())**float(num_2.get())
        num_3.set(result)
    elif opration=='درصد':
        result=float(num_1.get())/100
        num_3.set(result)
    elif opration=='%':
        result=float(num_1.get())%float(num_2.get())
        num_3.set(result)
#====================window===================================
window.mainloop()
کاربر 1920 دانلود Python

ارسال جواب

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


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

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

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