Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5929

Python • Re: AttributeError: 'App' object has no attribute 'button' tkinter and gpiozero

$
0
0
Hello All, I'm new to python and programming in general, I'm trying to create a GUI that changes between frames when I press a physical button on my raspberry pi, but I'm getting the error of App object has no attribute button. bellow the code

Code:

import tkinter as tkfrom gpiozero import Button as btnbutton = btn(4)class App:    def __init__(self, root=None):        self.root = root        self.frame = tk.Frame(self.root)        self.frame.pack()        tk.Label(self.frame, text='Main page').pack()self.button.when_pressed = self.make_page_1        self.page_1 = Page_1(master=self.root, app=self)    def main_page(self):        print(4)        self.frame.pack()    def make_page_1(self):        print(1)        self.frame.pack_forget()        self.page_1.start_page()class Page_1:    def __init__(self, master=None, app=None):        self.master = master        self.app = app        self.frame = tk.Frame(self.master)        tk.Label(self.frame, text='Page 1').pack()        tk.Button(self.frame, text='Go back', command=self.go_back).pack() #botton    def start_page(self):        print(2)        self.frame.pack()    def go_back(self):        print(3)        self.frame.pack_forget()        self.app.main_page()if __name__ == '__main__':    root = tk.Tk()    app = App(root)    root.mainloop()
any advice?
In the class App the __init__ function has the following line:

Code:

self.button.when_pressed = self.make_page_1
But self.button isn't created anywhere in the class, infact the only place button is used is the global that is created just before you defined the class App and which is never used. Maybe that line in __init__ should've been:

Code:

button.when_pressed = self.make_page_1

Statistics: Posted by Paeryn — Sun Aug 25, 2024 11:58 pm



Viewing all articles
Browse latest Browse all 5929

Latest Images

Trending Articles



Latest Images