Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python类中添加可选参数?_Python_Tkinter_Arguments_Python Class - Fatal编程技术网

如何在python类中添加可选参数?

如何在python类中添加可选参数?,python,tkinter,arguments,python-class,Python,Tkinter,Arguments,Python Class,我试图根据rb_选择值调用不同的函数,如果rb_选择值为0,则调用func1;如果rb_选择值为1,则调用func2。这两个函数采用不同的参数集 调用func1时不需要文件夹参数(func2值),同样,调用func2时也不需要批处理、术语参数(func1值) 当我试图调用第二个函数时,它抛出了以下错误,因为没有传递batch、term的值 Exception in Tkinter callback Traceback (most recent call last): File "C:\Use

我试图根据rb_选择值调用不同的函数,如果rb_选择值为0,则调用func1;如果rb_选择值为1,则调用func2。这两个函数采用不同的参数集

调用func1时不需要文件夹参数(func2值),同样,调用func2时也不需要批处理、术语参数(func1值)

当我试图调用第二个函数时,它抛出了以下错误,因为没有传递batch、term的值

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Himajak\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<ipython-input-13-02b5f954b815>", line 122, in tb_click
    ThreadedTask(self.queue,self.batch_name,self.term_name,self.course,self.rb_selection,self.folder).start()
AttributeError: 'GUI' object has no attribute 'batch_name'

请就如何解决此问题提出建议,提前感谢

没有可选参数的概念,您可以在创建函数时提供默认值,如

def __init__(self, queue,rb_selection ,term_name, folder, batch_name="default batch name"):

因此,在创建实例时不需要传递批处理名称。

通过可选参数,我的意思是这些变量并不总是由两个函数调用,因此我使用了这个术语。谢谢你的批改!!我已经尝试过用您提到的一些默认值来定义它,但它不起作用并抛出相同的错误。您在
class1
中遇到的错误是因为该变量未定义。
def __init__(self, queue,rb_selection ,term_name, folder, batch_name="default batch name"):