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

时间:2017-12-21 14:26:36

标签: python tkinter python-3.6 boolean-logic boolean-operations

我正在研究一个计算器,我想要它,所以一旦用户输入第一个数字并选择一个操作员(+ - * /等),他们可以输入第二个数字,然后点击等于按钮得到他们的答案。

目前,我无法在我的def内为我的bool分配一个值,这使我无法输入第二个数字并最终执行功能。 commandButtonPress()的最后一行是事情正在下滑的地方,在我whichNum list之前我正在获取:

  分配前引用的

变量

我尝试了直接分配以及使用remove()append()。按钮1-9使用相同的代码块,我没有包含小数,因为我认为没有必要解决这个问题。

我知道这一切都不优雅,如果我实现了一个类,大部分都会好得多,但我是新手并试图解决问题并且还没有在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()

1 个答案:

答案 0 :(得分:0)

Nae发布的解决方案似乎有效。我的按钮还没有正确处理num2,但是在commandButtonPress中,whichNum正在更新。谢谢您的帮助。我完成了这个项目(或多或少)并将其上传到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()