【问题标题】:AttributeError: 'Application' object has no attribute 'rp_toggle_button' [duplicate]AttributeError:“应用程序”对象没有属性“rp_toggle_button”[重复]
【发布时间】:2021-11-02 11:34:09
【问题描述】:

我试图用一些 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()

有谁知道为什么会发生这个错误以及我可以做些什么来解决它?

【问题讨论】:

  • 你不应该从其他线程调用tkinter 方法。 Python 可能会在不给您回溯的情况下崩溃。

标签: python multithreading tkinter


【解决方案1】:

我认为这是因为您没有在定义中创建/定义“rp_toggle_button”。但我不确定。

【讨论】:

  • 我不认为这是问题所在,因为当我没有尝试使用线程时它起作用了。
猜你喜欢
  • 2018-10-17
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 2021-04-06
  • 2011-09-18
  • 2021-05-26
  • 2019-11-22
  • 2019-03-18
相关资源
最近更新 更多