简体   繁体   English

处理不带参数但有时使用来自 bind 的事件调用的函数的最佳方法?

[英]Best way to handle a function that takes no arguments but sometimes is called with events from bind?

I know that I can use event=None and this works fine however PyCharm likes to complain about this as the event is never used in the function.我知道我可以使用event=None并且这很好用,但是 PyCharm 喜欢抱怨这个,因为该事件从未在函数中使用过。 What is the best way to satisfy PyCharm's inspector.满足 PyCharm 的检查员的最佳方法是什么。 For this notice:对于此通知:

在此处输入图片说明

Take this function:拿这个功能:

def funct(event=None):
   #  do stuff

Where I have a button command that calls it:我有一个调用它的按钮命令:

tk.Button(root, text='Ok', command=funct)

Or a bind that can call it:或者可以调用它的绑定:

root.bind(<Return>, funct)

Now because binds always sent an event what is the best way to tell the function to accept calls to it with or without an argument and wont trip PyCharm's inspector.现在因为绑定总是发送一个事件,所以最好的方法是告诉函数接受带参数或不带参数的调用并且不会跳闸 PyCharm 的检查器。

Am I missing something or is this inspector just overly cautious?我是不是遗漏了什么,还是这位检查员过于谨慎?

I could change the bind to use a lambda and then I can remove event=None from the function:我可以更改绑定以使用 lambda,然后我可以从函数中删除event=None

root.bind(<Return>, lambda e: funct)

That seams like over kill for this.那个接缝就像杀了这个。

I don't think I should just throw event in the function so PyCharm stops complaining that event is not used.我不认为我应该只在函数中抛出event ,这样 PyCharm 就不会抱怨没有使用event

I have tried also to use _ in the argument place however doing so requires some argument to be sent so I would have to perform a lambda in the button command for this to work and again feels like overkill.我也尝试在参数位置使用_但是这样做需要发送一些参数,所以我必须在按钮命令中执行 lambda 才能使其工作,再次感觉有点矫枉过正。

So that leads me to my question.所以这就引出了我的问题。

Is there a know method of handling this that I am not aware of or do I have to just deal with the inspectors message always on my screen?是否有一种我不知道的已知方法来处理这个问题,或者我是否必须始终处理屏幕上的检查员消息?

Note: I did try to remove this notice from the settings but I could not find the notice for this particular notification to remove...注意:我确实尝试从设置中删除此通知,但我找不到要删除此特定通知的通知...

For my needs the simple solution was to use _=None as mentioned by Iain Shelvington .对于我的需要,简单的解决方案是使用Iain Shelvington提到的_=None

However for completeness sake I combined the use of *args, **kwargs with _ to have a catch all that PyCharm does not complain about and will catch anything that is passed to the function be it positional or named.然而,为了完整起见,我结合使用*args, **kwargs_来捕获 PyCharm 没有抱怨的所有内容,并将捕获传递给函数的任何内容,无论是位置的还是命名的。

Example:例子:

def test(*_args, **_kwargs):
    print('test')


test()
test('1')
test(x='1')

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

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