Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Push My Buttons

Shawn Mathes
Shawn Mathes
1,826 Points

timer_text

So, I'm currently following allowing with the Tinter workshop and whenever I try to run this with the block of code listed below, I get an error on pycharm, AttributeError: 'Pymodoro' object has no attribute 'timer_text'. I've been trying my best to pinpoint the problem, but from what I can see everything looks right.

import tkinter

DEFAULT_GAP = 60 * 25


class Pymodoro:
    def __init__(self, master):
        self.master = master
        self.mainframe = tkinter.Frame(self.master, bg="white")
        self.mainframe.pack(fill=tkinter.BOTH, expand = True)

        self.build_grid()
        self.build_banner()
        self.build_buttons()
        self.build_timer()

        self.timer_text = tkinter.StringVar()
        self.time_left = tkinter.IntVar()
        self.time_left.set(DEFAULT_GAP)
        self.running = False # When the app starts it's not running

    def build_grid(self):
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=0)
        self.mainframe.rowconfigure(1, weight=1)
        self.mainframe.rowconfigure(2, weight=0)

    def build_banner(self):
        banner = tkinter.Label(
            self.mainframe,
            background='black',
            text='Pymodoro',
            fg='white',
            font=('Helvetica', 24)
        )
        banner.grid(
            row=0, column=0,
            sticky='ew',
            padx=10, pady=10
        )

    def build_buttons(self):
        buttons_frame = tkinter.Frame(self.mainframe)
        buttons_frame.grid(row=2, column=0, sticky='nsew', padx=10, pady=10)
        buttons_frame.columnconfigure(0, weight=1)
        buttons_frame.columnconfigure(1, weight=1)

        self.start_button = tkinter.Button(
            buttons_frame,
            text='Start',
            command=self.start_timer
        )
        self.stop_button = tkinter.Button(
            buttons_frame,
            text='Stop',
            command=self.stop_timer
        )

        self.start_button.grid(row=0, column=0, sticky='ew')
        self.stop_button.grid(row=0, column=1, sticky='ew')

    def build_timer(self):
        timer = tkinter.Label(
            self.mainframe,
            text=self.timer_text.get(),
            font=('Helvetica', 36)
        )

        timer.grid(row=1, column=0, sticky='nsew')

    def start_timer(self):
        self.time_left.set(DEFAULT_GAP)
        self.running = True

    def stop_timer(self):
        self.running = False

if __name__ == '__main__':
    root = tkinter.Tk()
    Pymodoro(root)
    root.mainloop()
Shawn Mathes
Shawn Mathes
1,826 Points

Well, I ended up figuring out a solution to the issue and it was because I was calling the methods before declaring the variables.

class Pymodoro:
    def __init__(self, master):
        self.master = master
        self.mainframe = tkinter.Frame(self.master, bg="white")
        self.mainframe.pack(fill=tkinter.BOTH, expand=True)

        self.timer_text = tkinter.StringVar()
        self.time_left = tkinter.IntVar()
        self.time_left.set(DEFAULT_GAP)
        self.running = False # When the app starts it's not running

        self.build_grid()
        self.build_banner()
        self.build_buttons()
        self.build_timer()