【问题标题】:Adding digits to a variable using tkinter buttons in python使用python中的tkinter按钮向变量添加数字
【发布时间】: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。你已经有什么代码了?
  • 您提供的示例不是最少的(复制问题所需的最少代码量),完整的(我看不到任何数字按钮)也不是可验证的(似乎没有是其中的任何错误。您能否为我们提供一个实际让我们看到您遇到的问题的示例?

标签: python tkinter


【解决方案1】:

我不会为你完成整个程序,但下面展示了如何使用按钮向变量添加数字,以及总体上更好的方式来布局你的 GUI。

为了方便将数字添加到workspace 标签,我创建了一个StringVar,并在创建它时使用textvariable= 选项将其关联到Label。为了实际添加数字,添加了一个名为 add_digit() 的函数,并创建了一个调用它的 lambda 函数并将其用作每个数字 Button 上的 command= 回调函数。

使用 tkinter 布局复杂 GUI 小部件的一个好方法是创建嵌套的 Frames 并将它们的组放在一个中。这允许通过包含Frame 来定位整个组。它还允许您在容器中安排事物,而不必担心是否放置了其他小部件。

from tkinter import *

operator = ''
current_problem = ''

# Function to change text in workspace
def append_to_workspace(item):
    """ Add item to workspace StringVar. """
    if not isinstance(item, str):
        item = str(item)
    ws_var.set(ws_var.get() + item)


# 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
ws_var = StringVar(value='')
workspace = Label(window, width=25, height=1, textvariable=ws_var)
workspace.grid(row=0, column=0)

# Operator keypad
operators = Frame(window)

add_button = Button(operators, text='+', width=2, command=add)
add_button.grid(row=1, column=0)
subtract_button = Button(operators, text='-', width=2, command=subtract)
subtract_button.grid(row=1, column=1)
divide_button = Button(operators, text='÷', width=2, command=divide)
divide_button.grid(row=1, column=2)
multiply_button = Button(operators, text='×', width=2, command=multiply)
multiply_button.grid(row=1, column=3)

operators.grid(row=1)

# Numeric keypad
numbers = Frame(window)

button7 = Button(numbers, text='7', width=2, command=lambda: append_to_workspace(7))
button7.grid(row=0, column=0)
button8 = Button(numbers, text='8', width=2, command=lambda: append_to_workspace(8))
button8.grid(row=0, column=1)
button9 = Button(numbers, text='9', width=2, command=lambda: append_to_workspace(9))
button9.grid(row=0, column=2)

button4 = Button(numbers, text='4', width=2, command=lambda: append_to_workspace(4))
button4.grid(row=1, column=0)
button5 = Button(numbers, text='5', width=2, command=lambda: append_to_workspace(5))
button5.grid(row=1, column=1)
button6 = Button(numbers, text='6', width=2, command=lambda: append_to_workspace(6))
button6.grid(row=1, column=2)

button1 = Button(numbers, text='1', width=2, command=lambda: append_to_workspace(1))
button1.grid(row=2, column=0)
button2 = Button(numbers, text='2', width=2, command=lambda: append_to_workspace(2))
button2.grid(row=2, column=1)
button3 = Button(numbers, text='3', width=2, command=lambda: append_to_workspace(3))
button3.grid(row=2, column=2)

button0 = Button(numbers, text='0', width=2, command=lambda: append_to_workspace(0))
button0.grid(row=3, column=0, columnspan=3)

numbers.grid(row=2)

window.mainloop()

这是在我的系统上运行的样子:

【讨论】:

    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2015-12-31
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-30
    • 2015-09-11
    相关资源
    最近更新 更多