有没有办法让tkinter中的输入框等待输入?(在Python 3中)

2024-10-05 14:24:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图根据教程(链接是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()

在“操作”方法中,我很难让我的程序等待用户输入他们想在计算器中使用的第二个数字

我一直在犯错误

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

我认为这是因为程序只是将输入框中的任何内容转换成一个整数,并将其与第一个数字相加,然后得到一个空格

因此,我尝试使用

time.sleep(20)

所以程序会等我输入另一个正整数一段时间,但这只是把GUI搞乱了

有人知道如何让tkinter等待输入吗?谢谢

这是我第一次使用堆栈溢出作为问题,请原谅任何错误


Tags: lambdatextcolumnbuttonrootdeletecommandtk
3条回答

您不应该使用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()

我认为,如果您的解决方案不破坏GUI,它也会很好。 函数的作用是:完全停止包含GUI的代码,因此我建议使用tkinter线程

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

这段视频将帮助您解决问题,搞乱GUI

首先,“操作”被称为函数,而不是方法。我建议你在谷歌搜索一下有什么不同

您不需要Tkinter等待进入。您可以在功能代码处进行检查。例如,检查条目是否存在,如果不存在,则不返回任何内容

例如:

if not ans.get():
    return

^^^^示例检查条目是否存在,如果不存在,则返回None并阻止函数抛出错误。如果你愿意,你可以阅读关于try的文章,当然也可以

相关问题 更多 >