Итак, я пытаюсь построить калькулятор на основе графического интерфейса, ничего особенного, только с базовыми операторами (сложение, вычитание, умножение и деление).Однако я пытаюсь использовать кнопки для ввода чисел, которые нужно сложить / вычесть, как настоящий калькулятор.Я использую переменную num1
для хранения первого числа и num2
для хранения второго числа, например, num1 + num2 = result
Однако я не знаю, как сказать, есликнопка 1 нажимается дважды, чтобы num1
содержало значение 11?
Если я хочу добавить от 12 до 43, мне нужен способ установки num1
в 12, нажав кнопку 1, а затем кнопку 2.Затем укажите моего оператора, нажав кнопку добавления, затем нажмите кнопку 4 и кнопку 3, чтобы установить num2
на 43.
Вот мой код (Примечание: я еще не закончил кнопки, помеченные как button1 - button0):
from tkinter import *
operator = ''
current_problem = ''
# Functions to change text in workspace
def add():
global current_problem
current_problem = current_problem + '+'
workspace.config(text = current_problem)
operator = 'A'
def subtract():
global current_problem
current_problem = current_problem + '-'
workspace.config(text = current_problem)
operator = 'S'
def divide():
global current_problem
current_problem + current_problem + '÷'
workspace.config(text = current_problem)
operator = 'D'
def multiply():
global current_problem
current_problem = current_problem + '×'
workspace.config(text = current_problem)
operator = 'M'
def num1():
global current_problem
current_problem = current_problem + '1'
workspace.config(text = current_problem)
# Create the main Tkinter Window
window = Tk()
window.title('Calculator')
# Add an empty Label for the workspace, place it in grid
workspace = Label(window, width = 25, height = 1, text = '')
workspace.grid(row = 0, column = 0)
add_button = Button(window, text = '+', width = 2, command = add)
add_button.grid(row = 1, column = 0)
subtract_button = Button(window, text = '-', width = 2, command = subtract)
subtract_button.grid(row = 1, column = 1)
divide_button = Button(window, text = '÷', width = 2, command = divide)
divide_button.grid(row = 1, column = 2)
multiply_button = Button(window, text = '×', width = 2, command = multiply)
multiply_button.grid(row = 1, column = 3)
button1 = Button(window, text = '×', width = 2, command = multiply)
button1.grid(row = 1, column = 3)
button2 = Button(window, text = '×', width = 2, command = multiply)
button2.grid(row = 1, column = 3)
button3 = Button(window, text = '×', width = 2, command = multiply)
button3.grid(row = 1, column = 3)
button4 = Button(window, text = '×', width = 2, command = multiply)
button4.grid(row = 1, column = 3)
button5 = Button(window, text = '×', width = 2, command = multiply)
button5.grid(row = 1, column = 3)
button6 = Button(window, text = '×', width = 2, command = multiply)
button6.grid(row = 1, column = 3)
button7 = Button(window, text = '×', width = 2, command = multiply)
button7.grid(row = 1, column = 3)
button8 = Button(window, text = '×', width = 2, command = multiply)
button8.grid(row = 1, column = 3)
button9 = Button(window, text = '×', width = 2, command = multiply)
button9.grid(row = 1, column = 3)
button0 = Button(window, text = '×', width = 2, command = multiply)
button0.grid(row = 1, column = 3)
Возможно ли это в Python?