属性错误:"应用程序"对象没有属性"rp_toggle_button"



我试图用一些GUI为Discord编写一个基本的GUI丰富的存在应用程序,我尝试使用线程用于当您按下按钮时调用的函数,但由于某种原因,当我向线程中使用的函数添加参数时,我得到一个错误说AttributeError: 'Application' object has no attribute 'rp_toggle_button'。这是我的代码:

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()

有没有人知道为什么这个错误发生,我能做什么来修复它?

我认为这是因为你没有制作/定义'rp_toggle_button',而不是在定义。但我不确定。

最新更新