使用 tkinter 按钮更新我的布尔值的最佳方法是什么?

What would be the best way to update my boolean using tkinter buttons?

我正在开发一个计算器,我想要它,所以一旦用户输入完第一个数字并选择一个运算符(+-*/等),他们就可以输入第二个数字,然后点击等号按钮得到他们的答案。

目前,我无法为 def 中的 bool 赋值,这使我无法输入第二个数字并最终执行函数。 commandButtonPress() 的最后一行是事情出错的地方,在将 whichNum 变成 list 之前,我得到:

variable referenced before assignment

我已经尝试过直接赋值以及使用 remove()append()。按钮 1-9 使用相同的代码块,我没有包括小数点,因为我认为解决这个问题没有必要。

我知道 none 这很优雅,如果我实现一个 class,其中大部分会好得多,但我是新手,正在尝试解决问题,但还没有完成 object面向编程python还没有。

from tkinter import *
def buttonOne(whichNum):
    count = 0
    number = ""
    try:
        if whichNum == False and num1[0] != "0":
            num1.append("1")
            while count < len(num1):
                number = number + str(num1[count])
                count += 1
            screen["text"] = number
        elif whichNum == True and num2[0] != "0":
            num2.append("1")
            while count < len(num2):
                number = number + str(num2[count])
                count += 1
            screen["text"] = number
    except:
        if whichNum == False:
            num1[0] = "."
        else:
            num2[0] = "."
        number = "0."
        screen["text"] = number
def commandButtonPress(symbol, whichNum):
    if whichNum == False:
        if len(operator) > 0 and operator[0] == "+":
            operator.remove("+")
    if len(num1) > 0:
        operator.append(symbol)
        whichNum[0] = True
def main():
    window = Tk()
    window.title("Calculator")
    window.geometry("250x305")
    num1 = []
    num2 = []
    operator = []
    whichNum = [False]

    global screen
    screen = Label(text="0", height = 5, width = 30)
    screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])

    button1 = Button(text="1", command = lambda: buttonOne(whichNum[0]))
    button1.grid(column=0, row=2)
    button11 = Button(text="+", command = lambda: commandButtonPress("+", whichNum[0]))
    button11.grid(column=3, row=2)
    window.mainloop()

main()

Nae 发布的解决方案似乎有效。我的按钮还没有正确处理 num2,但是 whichNum 正在更新,因为它应该在 commandButtonPress 中。谢谢您的帮助。我完成了这个项目(或多或少)并将其上传到 my repo

from tkinter import *
whichNum = False
def buttonOne():
    global whichNum
    count = 0
    number = ""
    try:
        if whichNum == False and num1[0] != "0":
            num1.append("1")
            while count < len(num1):
                number = number + str(num1[count])
                count += 1
            screen["text"] = number
        elif whichNum == True and num2[0] != "0":
            num2.append("1")
            while count < len(num2):
                number = number + str(num2[count])
                count += 1
            screen["text"] = number
    except:
        if whichNum == False:
            num1[0] = "."
        else:
            num2[0] = "."
        number = "0."
        screen["text"] = number
def commandButtonPress(symbol):
    global whichNum
    if whichNum == False:
        if len(operator) > 0 and operator[0] == "+":
            operator.remove("+")
    if len(num1) > 0:
        operator.append(symbol)
        whichNum[0] = True
def main():
    window = Tk()
    window.title("Calculator")
    window.geometry("250x305")
    num1 = []
    num2 = []
    operator = []
    global whichNum

    global screen
    screen = Label(text="0", height = 5, width = 30)
    screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])

    button1 = Button(text="1", command = lambda: buttonOne())
    button1.grid(column=0, row=2)
    button11 = Button(text="+", command = lambda: commandButtonPress("+"))
    button11.grid(column=3, row=2)
    window.mainloop()

main()