0

In the main file below I have created class objects (f1,f2) and a function (set_values) to access and modify class objects. And the above function is called in another file. But I am getting an error 'NameError: name 'f2' is not defined'. Any ideas...?

from  threading import Thread
import tkinter as tk
import temp_gui
import temp_friday_ai

if __name__ == '__main__':
    a = temp_friday_ai.welcome()
    temp_friday_ai.Speak_text_a(a)
    root = tk.Tk()
    root.geometry('350x550')
    root.title('FRIDAY')
    root.rowconfigure(0 , weight = 8)
    root.rowconfigure(1 , weight = 1)
    root.columnconfigure(0 , weight = 1)
    root.columnconfigure(1 , weight = 1)
    color1 = '#0e1221'
    color2 = '#423F3E'
    f1 = temp_gui.rootdUpper(root , color1)
    f2 = temp_gui.Chat(root ,color2)
    f2.create_widgets(f2.l)
    Thread(target= temp_friday_ai.friday_main, args= (root, f1, f2, )).start()
    root.mainloop()

def set_values(d):
    global f1 
    global f2
    for x in d.keys():
        if x =='theme':   
            if d[x] == 'Dark': color1 ,color2 , mode = '#0e1221' ,'#423F3E' ,'dark'
            else : color1 ,color2 , mode = '#b3c6c7' ,'#203030' ,'light'
            f2.theme_mode(color1,color2,mode)#calling class method
            f1.theme_mode(color1,color2,mode)
        elif x == 'gender':
            if d[x] =='Male': temp_friday_ai.engine.setProperty('voice', temp_friday_ai.voices[1].id)
            else: temp_friday_ai.engine.setProperty('voice', temp_friday_ai.voices[0].id)
        elif x == 'voice':
            if d[x] == 'Slow': r = 100
            elif d[x] == 'Normal': r = 150
            else: r = 200
            temp_friday_ai.engine.setProperty('rate' , r)
Alex Waygood
  • 4,796
  • 3
  • 14
  • 41
Vicky
  • 1
  • Try removing `if __name__ == '__main__':` line – Hirusha Fernando Sep 25 '21 at 06:22
  • 2
    Also note that is example of the reasons why using global variables is considered bad. You import function that rely on global variables. – buran Sep 25 '21 at 06:28
  • @HirushaFernando fun set_values is being called in another file if main guard is removed whole block gets executed when imported.. can u pls suggest how to access those class objects from another file – Vicky Sep 25 '21 at 10:37
  • Is there any reason you can't move all the code from the `if __name__ == "__main__"` block and put it in the `set_values()` function? It seems that you only want it to run when the function is called anyway. If you also need the program to run on it's own, you could have `if __name__ == "__main__": set_values()`. A better approach, however, would be to just use a class and have `set_values()` as one of its methods. As [buran](https://stackoverflow.com/users/4046632/buran) said, using global variables for importable functions is a bad idea. – Sylvester Kruin Sep 25 '21 at 18:18

0 Answers0