简体   繁体   English

单击 Tkinter 中的按钮时程序无响应?

[英]Program unresponsive on clicking a button in Tkinter?

I'm trying to create a simple calculator using Tkinter, but I'm facing a problem.我正在尝试使用 Tkinter 创建一个简单的计算器,但我遇到了一个问题。 First see the relevant code:先看相关代码:

entry_text = StringVar()
inout = Entry(root, textvariable=entry_text)
inout.grid(row=0, column=0, columnspan=4, sticky="nsew")

def equals():
    print("Equal button is clicked")
    get_answer = True

def divide():
    tempvar = entry_text.get()
    num1 = int(tempvar)
    entry_text.set("")
    while get_answer == False:
        tempvar2 = entry_text.get()
        try:
            num2 = int(tempvar2)
        except ValueError:
            num2 = 0
    print("I'm out of the loop.")
    answer = num1 / num2
    entry_text.set(answer)

Here I'm creating a function for the divide button.在这里,我正在为divide按钮创建一个函数。 The functionality of the button is whenever you click on the button, it takes the instantaneous value of the entry_text variable, stores it in a temporary variable and resets the value of entry_text variable.按钮的功能是每当您单击按钮时,它都会获取entry_text变量的瞬时值,将其存储在临时变量中并重置entry_text变量的值。 Then it runs a loop for collecting the next value of entry_text until the equal button is clicked.然后它运行一个循环来收集entry_text的下一个值,直到单击等于按钮。 But the problem lies just here.但问题就在这里。 Whenever I click on divide button, the GUI becomes unresponsive, and I don't get to enter the next value for the divide operation and get out of the loop.每当我单击divide按钮时,GUI 都会变得无响应,并且我无法输入除法操作的下一个值并退出循环。

Can anyone help?任何人都可以帮忙吗?

Avoid using while loop in a tkinter application because it will block tkinter mainloop from handling pending events.避免在mainloop应用程序中使用 while 循环,因为它会阻止mainloop处理挂起的事件。

Also, get_answer inside equals() is a local variable because you haven't declare it as a global using global get_answer .此外, equals()中的get_answer是一个局部变量,因为您尚未使用global get_answer其声明为global get_answer

Actually you should perform the required operation inside equals() , but you need to store the first number and selected operation as global variables:实际上,您应该在equals()执行所需的操作,但您需要将第一个数字和选定的操作存储为全局变量:

num1 = 0
operator = None

def equals():
    global num1, operator
    print("Equal button is clicked")
    try:
        tempvar = entry_text.get()
        num2 = float(tempvar)  # used float() instead of int()
        if operator == '/' and num2 != 0:
            answer = num1 / num2
            entry_text.set(answer)
            operator = None # reset operator
    except ValueError:
        print('Invalid value', tempvar)

def divide():
    global num1, operator
    try:
        tempvar = entry_text.get()
        num1 = float(tempvar)  # used float() instead of int()
        entry_text.set("")
        operator = '/'   # save the operator
    except ValueError:
        print('Invalid value', tempvar)

The program becomes unresponsive as the while loop continues forever and never breaks, as the variable get_answer is never changed to True .程序变得无响应,因为while循环永远持续下去并且永远不会中断,因为变量get_answer永远不会更改为True

You can't click on any button, because the while loop keeps running and can't break without the condition given to it turns false or it is manually told to break after some number of loops.你不能点击任何按钮,因为while循环一直在运行并且不能在没有给定条件变为false的情况下中断,或者在一定数量的循环后被手动告知break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM