如果在较早的功能之一中触发异常,请停止在Python中运行一系列功能
我有一组功能,根据用户输入,它被称为一个函数。我正在使用此代码来运行这些函数:
#Run Multiple Functions
def MultipleFunctions(*funcs):
def MultipleFunctions(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return MultipleFunctions
我正在调用与TKINTER对象绑定的命令行的函数。例如:
Op1 = OptionMenu(MainForm, Type, *Options, command=MultipleFunctions(Func1, Func2, Func3, Func4))
func1至4进行各种简单检查,主要是为了捕获非法选项选择。例如,func1检查编辑框的输入:
#Check SideA Box Length is Valid
def Func1(event):
try:
float(SideA.get())
except ValueError:
messagebox.showerror(message="This box only accepts numbers between 0 and 100")
SideA.focus()
if float(SideA.get()) > 100:
messagebox.showerror(message="This box only accepts numbers between 0 and 100")
SideA.focus()
如果在say func1中触发异常,我想做的是脱离多个功能。目前,它运行所有四个功能,无论如何。
有人可以将我指向正确的方向吗?
谢谢
I have a set of functions which, are being called one after another depending on user input. I'm using this code to run the functions:
#Run Multiple Functions
def MultipleFunctions(*funcs):
def MultipleFunctions(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return MultipleFunctions
I am calling the functions with command lines tied to tkinter objects. For instance:
Op1 = OptionMenu(MainForm, Type, *Options, command=MultipleFunctions(Func1, Func2, Func3, Func4))
Func1 to 4 run various simple checks mostly to catch illegal option selections. For example Func1 checks the input of an edit box:
#Check SideA Box Length is Valid
def Func1(event):
try:
float(SideA.get())
except ValueError:
messagebox.showerror(message="This box only accepts numbers between 0 and 100")
SideA.focus()
if float(SideA.get()) > 100:
messagebox.showerror(message="This box only accepts numbers between 0 and 100")
SideA.focus()
What I want to do is break out of the Multiple Functions routine if an exception is triggered in say Func1. At the moment it runs all four functions regardless.
Can somebody point me in the right direction please?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码正在处理除块以外的例外。
如果例外弹出,则有几种方法可以停止执行。
You code is handling exception in except block.
There are few ways on how to stop execution if exception pops up.
在函数调用和添加
try-except
添加一个断路语句以退出循环。注意:在装饰器中,请勿
进一步使用同一函数的名称,您需要重新实现该功能的主体为 rise 例外:
从评论中进行编辑:这是
func1
的方式应该看起来像(我没有TKINTER的经验)Add a
try-except
around the function call and add a break statement to quit the loop.Notice: in the decorator don't use the same function's name
Further you need to reimplement the body of the function to raise exceptions:
EDIT From the comments: this is how
Func1
should look like (I have no experience with tkinter)