0

I was trying to code a basic GUI rich presence app for Discord with some GUI and I tried use threads for the functions that are called when you press a button but for some reason when I add arguments to the function used in the thread, I get an error saying AttributeError: 'Application' object has no attribute 'rp_toggle_button'. This is my code:

from tkinter import *
import presence
import threading

class Application():
    def __init__(self, master):
        self.master = master
        master.title("New Year CountDown")
        master.geometry("300x200")
        self.add_widgets()

    def add_widgets(self):
        app_title = Label(text = "New Year Rich Presence for Discord")
        app_title.grid(row = 0, column = 0, columnspan = 2, sticky = W)

        select_unit_label = Label(text = "Select preferred time units: ", )
        select_unit_label.grid(row = 1, column = 0, sticky = W)

        options = ["days", "hours", "auto"]
        options_tk = StringVar(root)
        options_tk.set(options[0])
        time_unit_selector = OptionMenu(self.master, options_tk, *options)
        time_unit_selector.grid(row = 1, column = 1, sticky = W)

        #this is the button that is apparently not an attribute of Application
        self.rp_toggle_button = Button(text = "Start Rich Presence", activeforeground = "#808080", command = threading.Thread(target = self.rp_toggle, args = (options_tk,)).start())
        self.rp_toggle_button.grid(row = 2, column = 0, sticky = W)
    
    def rp_toggle(self, options):
        print("rp_toggle")
        if self.rp_toggle_button["text"] == "Start Rich Presence":
            try:
                presence.connect_rpc()
                presence.update_rpc(options.get())
                self.rp_toggle_button["text"] = "Stop Rich Presence"
            except ConnectionRefusedError:
                print("Connection Refused Error")
        else:
            self.rp_toggle_button["text"] = "Start Rich Presence"
            presence.update_rpc("clear")

root = Tk()
application = Application(root)
root.mainloop()

Does anyone know why this error is happening and what I can do to fix it?

OmarMZ
  • 13
  • 4

1 Answers1

-1

I think it is because you have not made/defined 'rp_toggle_button' other than in the define. But I am not to sure.

Aidan
  • 42
  • 6