0

I'm trying to create a Tkinter app using classes but I'm running into an issue being able to change the button state to disabled after clicking the button. The error I'm currently getting is: AttributeError: 'Menu' object has no attribute 'menu_button'

I'm new to Tkinter, though I think I'm failing more on my understanding of classes. So if in addition to helping me to get my code to work if there are some additional resources you recommend to strengthen my understanding I'd appreciate it.

Here is my current code:

import tkinter as tk
from tkinter import ttk

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.geometry("425x350")
        self.title("Poly Calculator")

        self.menu = Menu(self)

        self.mainloop()


class Menu(ttk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.place(x=0, y=0)

        menu_button = ttk.Button(self, text = 'Button 1',  command=self.change_state)
        menu_button.pack()

    def change_state(self):
        self.menu_button['state'] = 'disabled'
        
App()
0

1 Answer 1

0

menu_button is just a local variable in the __init__ function. If you want it to be a member variable, which you do, then you need

        self.menu_button = ttk.Button(self, text = 'Button 1',  command=self.change_state)
        self.menu_button.pack()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.