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

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

-1
-1

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

Admin آسان 33/ 1015 بازدید

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

4 جواب

نمیتونم این تمرین رو حل کنم!
0
+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
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
-1
-1
-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

ارسال جواب

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


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

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

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