使用消息框尝试 tkinter 中的异常
我想尝试 tkinter 中的异常。如果我没有在条目中输入任何内容,它将引发 ValueError 并弹出消息框。然而,当我运行我的代码时,它确实可以捕获异常,但没有弹出消息框。谁能告诉我原因以及如何纠正?谢谢。
from tkinter import *
from tkinter import messagebox
try:
root = Tk()
def function():
if not entry_test.get():
raise ValueError
Label(root, text = entry_test.get()).grid()
entry_test = Entry(root)
entry_test.grid()
button_click = Button(root, text ="click to test", command= function)
button_click.grid()
root.mainloop()
except ValueError:
message.showwarning("ENTRY ERROR", "please input somthing in entry")
I want to try exception in tkinter. If I do not input anything into entry, it will raise ValueError and the messagebox will pop up. However, when I run my code, it indeed can catch exception, but no messagebox pops up. Can anyone tells me reason and how to correct it? Thank you.
from tkinter import *
from tkinter import messagebox
try:
root = Tk()
def function():
if not entry_test.get():
raise ValueError
Label(root, text = entry_test.get()).grid()
entry_test = Entry(root)
entry_test.grid()
button_click = Button(root, text ="click to test", command= function)
button_click.grid()
root.mainloop()
except ValueError:
message.showwarning("ENTRY ERROR", "please input somthing in entry")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论