-2

I am trying to call different functions based on the value for rb_selection, calling func1 if rb_selection value is 0 and calling func2 if rb_selection value is 1. Both functions take a different set of arguments.

I do not need folder argument(func2 values) when I call func1 and similarly I do not need batch, term arguments(func1 values) when I call func2

It throws me the below error when I try to call the second function, as the values for batch, term are not passed.

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'

Code looks similar to this:

class class1():

    def def1(self):

        self.queue = queue.Queue()
        ThreadedTask(self.queue,self.rb_selection,self.batch_name,self.folder).start()
        #self.master.after(10, self.process_queue)


class class2():

    def __init__(self, queue,rb_selection, batch_name ,term_name, folder):
        threading.Thread.__init__(self)
        self.queue = queue
        self.rb_selection = rb_selection
        self.batch = batch_name
        self.term = term_name
        self.folder = folder


    def func1(self,batch,term):
        time.sleep(5)
        print("Fucntion 1 reached")
        print(self.batch,self.term)


    def func2(self,folder):
        time.sleep(5)
        print("Function 2 reached")
        print(self.folder)

    def run(self):
        time.sleep(0) # Simulate long running process

        if self.rb_selection == '0':
            self.func1(self.batch,self.term)
        elif self.rb_selection == '1':
            self.func2(self.folder)

        self.queue.put("Task finished")

Please suggest on how to resolve this issue, thanks in advance!

shubhasreepv
  • 51
  • 1
  • 6

1 Answers1

0

There is no concept of optional arguments, you can give default value when creating the function like

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

So that you need not pass batch_name while creating the Instance.

Prudhvi
  • 1,095
  • 1
  • 7
  • 18
  • By optional arguments I meant those variables are not always called by both the functions, so I have used that terminology. Thanks for correcting !! I have already tried defining it with some default value as you mentioned, it does not work and throws the same error. – shubhasreepv Apr 23 '20 at 11:27
  • That error you are getting in `class1` because that variable is not defined. – Prudhvi Apr 23 '20 at 11:50