有什么办法可以让 tkinter 中的输入框等待输入? (在 Python 3)

Is there any way you can make an entry box in tkinter wait for input? (in Python 3)

我正在尝试根据教程(link 是 https://www.youtube.com/watch?v=YXPyB4XeYLA)为计算器制作 GUI,但我想自己创建计算器逻辑。

我的所有代码都在这里:

import tkinter as tk

root = tk.Tk()

root.title('Calculator')

ans = tk.Entry(root, borderwidth=4, width=40)
ans.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

def typeNum(number):
    current = ans.get()
    ans.delete(0, tk.END)

    global firstNum

    ans.insert(0, str(current) + str(number))

    firstNum = int(str(current) + str(number))

def eq():
    eqClicked = True

def operation(op):
    opClicked = True
    secNum = 0

    if op == '+':

        ans.delete(0, tk.END)
        ans.wait_var(secNum)
        secNum = int(ans.get())

        eq()
        
        ans.delete(0, tk.END)
        ans.insert(0, firstNum + secNum)

    elif op == '-':
        
        ans.delete(0, tk.END)
        ans.wait_var(secNum)
        secNum = int(ans.get())

        eq()
        
        ans.delete(0, tk.END)
        ans.insert(0, firstNum - secNum)

    elif op == '*':
        
        ans.delete(0, tk.END)
        ans.wait_var(secNum)
        secNum = int(ans.get())

        eq()
        
        ans.delete(0, tk.END)
        ans.insert(0, firstNum * secNum)

    elif op == '/':
        ans.delete(0, tk.END)
        ans.wait_var(secNum)
        secNum = int(ans.get())

        eq()
        
        ans.delete(0, tk.END)
        ans.insert(0, firstNum / secNum)

def clearAll():
    ans.delete(0, tk.END)



button1 = tk.Button(root, text='1', padx=40, pady=20, command=lambda: typeNum(1))
button2 = tk.Button(root, text='2', padx=40, pady=20, command=lambda: typeNum(2))
button3 = tk.Button(root, text='3', padx=40, pady=20, command=lambda: typeNum(3))
button4 = tk.Button(root, text='4', padx=40, pady=20, command=lambda: typeNum(4))
button5 = tk.Button(root, text='5', padx=40, pady=20, command=lambda: typeNum(5))
button6 = tk.Button(root, text='6', padx=40, pady=20, command=lambda: typeNum(6))
button7 = tk.Button(root, text='7', padx=40, pady=20, command=lambda: typeNum(7))
button8 = tk.Button(root, text='8', padx=40, pady=20, command=lambda: typeNum(8))
button9 = tk.Button(root, text='9', padx=40, pady=20, command=lambda: typeNum(9))

button0 = tk.Button(root, text='0', padx=40, pady=20, command=lambda: typeNum(0))
equals = tk.Button(root, text='=', padx=40, pady=20, command=eq)
clear = tk.Button(root, text='CLEAR', padx=23, pady=20, command=clearAll)

add = tk.Button(root, text='+', padx=30, pady=20, command=lambda: operation('+'))
sub = tk.Button(root, text='-', padx=30, pady=20, command=lambda: operation('-'))
mult = tk.Button(root, text='x', padx=30, pady=20, command=lambda: operation('*'))
div = tk.Button(root, text='÷', padx=30, pady=20, command=lambda: operation('/'))

button1.grid(row=1, column=0)
button2.grid(row=1, column=1)
button3.grid(row=1, column=2)

button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)

button7.grid(row=3, column=0)
button8.grid(row=3, column=1)
button9.grid(row=3, column=2)

button0.grid(row=4, column=0)
equals.grid(row=4, column=1)
clear.grid(row=4, column=2)

add.grid(row=1, column=3)
sub.grid(row=2, column=3)
mult.grid(row=3, column=3)
div.grid(row=4, column=3)

root.mainloop()

在 'operation' 方法中,我无法让我的程序等待用户输入他们想在计算器中使用的第二个数字。

我一直收到

的错误
ValueError: invalid literal for int() with base 10: ''

我认为这是因为程序只是获取输入框中的任何内容,并试图将其转换为一个整数,以便与第一个数字相加,但它得到了一个空白 space .

正因为如此,我尝试使用

time.sleep(20)

这样程序会等待一段时间让我输入另一个正整数,但这只会弄乱 GUI。

有谁知道如何让 tkinter 等待输入?谢谢!

第一次使用Stack Overflow做题,如有错误请多多包涵。

首先,'operation'调用的是函数,不是方法。我建议您在 Google.

搜索有什么不同

您不需要 Tkinter 等待输入。您可以在功能代码中查看它。例如检查一个条目是否存在,如果不存在则 return 什么都没有。

例如:

if not ans.get():
    return

^^^ 该示例检查条目是否存在,如果不存在,它将 return None 并阻止函数抛出错误。如果你愿意,你可以阅读 try,except as well.

我认为如果您的解决方案没有弄乱 GUI,它也可以。 Time.sleep() 函数完全停止包括 GUI 在内的代码,因此我建议使用带 tkinter 的线程。

https://www.youtube.com/watch?v=FwKQwx91NAM

此视频将帮助您解决 GUI 问题。

您不应该使用 sleep 之类的东西任意等待输入,并且尝试在 operation 函数中读取另一个值似乎是错误的逻辑方法。如果您利用 tkinter 控件的事件驱动特性并稍微抽象您的代码,则可以完全避免等待输入。

这是一个非常简单的解决方案,它基于您的原始程序,但经过修改以在按顺序计算操作数和运算符之前将它们存储在数组中。如果用户输入无效的操作顺序,可能会出现一些边缘情况,但我尝试适应明显的情况。

显着的区别是此代码不尝试评估 operation 例程中的任何内容。它会存储操作数和运算符,直到输入 =,即计算发生时。

import tkinter as tk

root = tk.Tk()
root.title('Calculator')
ans = tk.Entry(root, borderwidth=4, width=40)
ans.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

data = []
tmpnum = None

def typeNum(number):
    global tmpnum
    current = ans.get()
    ans.delete(0, tk.END)
    ans.insert(0, str(current) + str(number))
    tmpnum = int(str(current) + str(number))

def eq():
    global tmpnum
    global data
    if tmpnum:
        data.append(tmpnum)
    print("DEBUG")
    print(data)

    # Evaluate
    if len(data) < 3:
        print("ERROR: incomplete equation")
        data.clear()
        return

    firstnum = data.pop(0)
    operator = data.pop(0)
    secondnum = data.pop(0)
    data.clear()

    if operator == "+":
        answer = firstnum + secondnum
    elif operator == "-":
        answer = firstnum - secondnum
    elif operator == "*":
        answer = firstnum * secondnum
    elif operator == "/":
        answer = firstnum / secondnum

    ans.delete(0, tk.END)
    ans.insert(0, str(answer))
    tmpnum = answer # Store result in case you want to use it for next operation

def operation(op):
    global tmpnum
    global data
    ans.delete(0, tk.END)
    if tmpnum:
        data.append(tmpnum)
        tmpnum = None
        data.append(op)

def clearAll():
    global data
    ans.delete(0, tk.END)
    data.clear()



button1 = tk.Button(root, text='1', padx=40, pady=20, command=lambda: typeNum(1))
button2 = tk.Button(root, text='2', padx=40, pady=20, command=lambda: typeNum(2))
button3 = tk.Button(root, text='3', padx=40, pady=20, command=lambda: typeNum(3))
button4 = tk.Button(root, text='4', padx=40, pady=20, command=lambda: typeNum(4))
button5 = tk.Button(root, text='5', padx=40, pady=20, command=lambda: typeNum(5))
button6 = tk.Button(root, text='6', padx=40, pady=20, command=lambda: typeNum(6))
button7 = tk.Button(root, text='7', padx=40, pady=20, command=lambda: typeNum(7))
button8 = tk.Button(root, text='8', padx=40, pady=20, command=lambda: typeNum(8))
button9 = tk.Button(root, text='9', padx=40, pady=20, command=lambda: typeNum(9))

button0 = tk.Button(root, text='0', padx=40, pady=20, command=lambda: typeNum(0))
equals = tk.Button(root, text='=', padx=40, pady=20, command=eq)
clear = tk.Button(root, text='CLEAR', padx=23, pady=20, command=clearAll)

add = tk.Button(root, text='+', padx=30, pady=20, command=lambda: operation('+'))
sub = tk.Button(root, text='-', padx=30, pady=20, command=lambda: operation('-'))
mult = tk.Button(root, text='x', padx=30, pady=20, command=lambda: operation('*'))
div = tk.Button(root, text='÷', padx=30, pady=20, command=lambda: operation('/'))

button1.grid(row=1, column=0)
button2.grid(row=1, column=1)
button3.grid(row=1, column=2)

button4.grid(row=2, column=0)
button5.grid(row=2, column=1)
button6.grid(row=2, column=2)

button7.grid(row=3, column=0)
button8.grid(row=3, column=1)
button9.grid(row=3, column=2)

button0.grid(row=4, column=0)
equals.grid(row=4, column=1)
clear.grid(row=4, column=2)

add.grid(row=1, column=3)
sub.grid(row=2, column=3)
mult.grid(row=3, column=3)
div.grid(row=4, column=3)

root.mainloop()