【发布时间】:2019-03-14 11:12:34
【问题描述】:
所以,我正在尝试构建一个基于 GUI 的计算器,没什么特别的,只是使用基本的运算符(加、减、乘和除)。但是我正在尝试使用按钮输入要加/减的数字,就像一个真正的计算器一样。我使用变量num1 来存储第一个数字,并使用num2 来存储第二个数字,例如,
num1 + num2 = result
但是我不知道怎么说,如果按钮 1 被按下两次,让num1 包含值 11?
如果我想将 12 添加到 43,我需要一种将 num1 设置为 12 的方法,方法是按 button1,然后按 button2。然后通过按添加按钮指定我的操作员,然后按按钮 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 中可行吗?
【问题讨论】:
-
不清楚您的问题是什么或您已经有什么问题。请考虑创建一个minimal reproducible example。
-
@ChristianDean 我添加了一个例子
-
我理解您的示例@DaneBaird,但您能否在您的问题中添加minimal reproducible example。你已经有什么代码了?
-
您提供的示例不是最少的(复制问题所需的最少代码量),完整的(我看不到任何数字按钮)也不是可验证的(似乎没有是其中的任何错误。您能否为我们提供一个实际让我们看到您遇到的问题的示例?