简体   繁体   English

当用户在 tkinter 输入框中没有输入时如何避免错误?

[英]How to avoid error when user enters no input in a tkinter entry box?

Hi I am trying to get a solution for, if a user enters no value into an entry box, it doesn't create an error.您好我正在尝试解决,如果用户在输入框中没有输入任何值,它不会产生错误。 i have tried:我努力了:

if players_math_answer_var.get() is None or players_math_answer_var.get() == "":
    players_math_answer_var.set(0)

also tried to set the value to 0 when initializing tk variable还尝试在初始化 tk 变量时将值设置为 0

[![players_math_answer_var = tk.IntVar(value=0)][1]][1]

But still get this error message (See screen shot)但仍然收到此错误消息(请参阅屏幕截图)

在此处输入图像描述

As I understand you want it to be impossible for errors to occur, so try the try - except statement据我了解,您希望不可能发生错误,因此请尝试 try - except 语句

try : # do this 
    # .....
except : # if error happened do this 
    # .....

暂无
暂无

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

相关问题 当用户单击 tkinter 中的错误消息框时,聚焦空输入框 - Focusing the empty entry box when user clicks the error messagebox in tkinter 当用户在 Tkinter 条目小部件中输入表情符号时,在条目上调用 .get 时会抛出异常 - When a user enters an emoji into the Tkinter entry widget, an exception is thrown when .get is called on the entry 如何加载 tkinter 的条目小部件中的 url,该小部件与列表框中列表的已保存用户输入链接? - How to load a url which is in entry widget of tkinter which is linked with saved user input of a list in list-box? 用 tkinter 单击时是否可以调整输入框(条目)的大小? - Is it possible to resize an input box (entry) when clicked with tkinter? 如何更改输入框用户输入 - How to change entry box user input 从 tkinter 中的条目计算平均值时如何避免除以零错误 - How to avoid division by zero error when calculating average value from entry in tkinter 如何从 tkinter 输入框获取输入到另一个 function 中的变量? - How to get input from a tkinter entry box, to a variable in another function? 如何通过输入框 tkinter Python 输入/使用浮点数? - How to input/use float through Entry box tkinter Python? Tkinter - 询问用户 integer 输入,直到他们在 tkinter 输入框中给出有效响应 - Tkinter - Asking the user for integer input until they give a valid response in tkinter entry box 当用户使用下面的代码在前两个实例中输入字符串时,如何避免“ZeroDivisionError:除以零”错误? - How do I avoid the 'ZeroDivisionError: division by zero' error when a user enters a string the first two instances using my code below?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM