Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

можешь на русском?

  1. User

    можешь на русском?

  2. Assistant

    Да, я могу на русском. Чем я могу Вам помочь?

  3. User

    rom tkinter import * from tkinter import messagebox import math def add_digit(digit): value = calc.get() if value[0] == '0' and len(value) == 1: value = value[1:] calc.delete(0, END) calc.insert(0, value + digit) def add_operation(operation): value = calc.get() if value[-1] in '-+/': value = value[:-1] elif '+' in value or '-' in value or '' in value or '/' in value: calculate() value = calc.get() calc.delete(0, END) calc.insert(0, value + operation) def calculate(): value = calc.get() if value[-1] in '-+/': value = value + value[:-1] calc.delete(0, END) try: calc.insert(0, eval(value)) except (NameError, SyntaxError): messagebox.showerror('Внимание!', 'Нужно вводить только числа и цифры!') calc.insert(0, 0) except ZeroDivisionError: messagebox.showerror('Внимание!', 'На ноль делить нельзя!') calc.insert(0, 0) def add_sqrt(): value = calc.get() value = float(value) value = math.sqrt(value) calc.delete(0, END) calc.insert(0, value) def add_fabs(): value = calc.get() value = eval(value) value = math.fabs(value) calc.delete(0, END) calc.insert(0, value) def clear(): calc.delete(0, END) calc.insert(0, 0) def make_calc_button(operation): return Button(text=operation, bd=5, font=('Times New Roman', 13), command=calculate) def make_clear_button(operation): return Button(text=operation, bd=5, font=('Times New Roman', 13), command=clear) def make_operation_button(operation): return Button(text=operation, bd=5, font=('Times New Roman', 13), command=lambda: add_operation(operation)) def make_sqrt_button(operation): return Button(text=operation, image=img, bd=5, font=('Times New Roman', 13), command=add_sqrt) def make_fabs_button(operation): return Button(text=operation, bd=5, font=('Times New Roman', 13), command=add_fabs) def make_digit_button(digit): return Button(text=digit, bd=5, font=('Times New Roman', 13), command=lambda: add_digit(digit)) tk=Tk() tk.geometry('260x360+100+200') tk.resizable(0,0) tk.title("Калькулятор") tk['bg']='#FFC0CB' calc = Entry(tk, justify=RIGHT, font=('Times New Roman', 15), width=15) calc.insert(0, '0') calc.place(x=15, y=20, width=220, height=30) make_digit_button('1').place(x=20, y=250, width=40, height=40) make_digit_button('2').place(x=80, y=250, width=40, height=40) make_digit_button('3').place(x=140, y=250, width=40, height=40) make_digit_button('4').place(x=20, y=190, width=40, height=40) make_digit_button('5').place(x=80, y=190, width=40, height=40) make_digit_button('6').place(x=140, y=190, width=40, height=40) make_digit_button('7').place(x=20, y=130, width=40, height=40) make_digit_button('8').place(x=80, y=130, width=40, height=40) make_digit_button('9').place(x=140, y=130, width=40, height=40) make_digit_button('0').place(x=20, y=310, width=100, height=40) make_digit_button('.').place(x=140, y=310, width=40, height=40) make_operation_button('+').place(x=200, y=310, width=40, height=40) make_operation_button('-').place(x=200, y=250, width=40, height=40) make_operation_button('').place(x=200, y=190, width=40, height=40) make_operation_button('/').place(x=200, y=130, width=40, height=40) make_fabs_button('|x|').place(x=140, y=70, width=40, height=40) make_clear_button('C').place(x=20, y=70, width=40, height=40) make_calc_button('=').place(x=200, y=70, width=40, height=40) tk.mainloop()

  4. Assistant

    Очень рад помочь! Это код Python для создания простого калькулятора на графическом интерфейсе с помощью библиотеки Tkinter. Калькулятор имеет кнопки для ввода цифр и математических операций, а также дополнительные функции, такие как вычисление модуля числа и извлечение корня.

  5. User

    создать графический калькулятор, простой, который может только складывать и вычитать

  6. Assistant

    Конечно, вот код для создания простого калькулятора на графическом интерфейсе, который может только складывать и вычитать:

    from tkinter import *
    
    # создаем функцию, которая будет складывать или вычитать
    def add(num1, num2):
        return num1 + num2
    
    def subtract(num1, num2):
        return num1 - num2
    
    # создаем функцию для кнопок
    def button_click(number):
        current = entry.get()
        entry.delete(0, END)
        entry.insert(0, str(current) + str(number))
    
    def button_add():
        first_number = entry.get()
        global operation
        operation = "add"
        global f_num
        f_num = int(first_number)
        entry.delete(0, END)
    
    def button_subtract():
        first_number = entry.get()
        global operation
        operation = "subtract"
        global f_num
        f_num = int(first_number)
        entry.delete(0, END)
    
    def button_equal():
        second_number = entry.get()
        entry.delete(0, END)
    
        if operation == "add":
            entry.insert(0, add(f_num, int(second_number)))
        elif operation == "subtract":
            entry.insert(0, subtract(f_num, int(second_number)))
                  
    # создаем окно и добавляем название
    window = Tk()
    window.title("Простой Калькулятор")
    
    # создаем окно ввода
    entry = Entry(window, width=35, borderwidth=5)
    entry.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
    
    # создаем кнопки
    button_1 = Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
    button_2 = Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
    button_3 = Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
    button_4 = Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
    button_5 = Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
    button_6 = Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
    button_7 = Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
    button_8 = Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
    button_9 = Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
    button_0 = Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
    button_add = Button(window, text="+", padx=39, pady=20, command=button_add)
    button_subtract = Button(window, text="-", padx=41, pady=20, command=button_subtract)
    button_equal = Button(window, text="=", padx=91, pady=20, command=button_equal)
    
    # вычисляем расположение кнопок
    button_1.grid(row=1, column=0)
    button_2.grid(row=1, column=1)
    button_3.grid(row=1, 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=3, column=0)
    button_8.grid(row=3, column=1)
    button_9.grid(row=3, column=2)
    
    button_0.grid(row=4, column=0)
    button_add.grid(row=5, column=0)
    button_subtract.grid(row=6, column=0)
    
    button_equal.grid(row=4, column=1, columnspan=2)
    
    # запускаем GUI
    window.mainloop()
    

    Калькулятор может выполнить сложение и вычитание, включая отрицательные числа. У вас есть кнопки «+», «-», «1», «2», «3», «4», «5», «6», «7», «8», «9», «0» и «=». Кнопка «=» вычисляет результат и выводит его в окно ввода.