Brandyn
Brandyn

Reputation: 1017

Python timer not displaying text as expected

i decided to build from a timer a found on this website, and before I did anything to it to make sure it looked ok, I decided to test it however the label that I have set to display is not working. Please be aware some thing may seem illogical, however I was just trying various things to try and get it to work

Code:

import Tkinter

class Timer(Tkinter.Tk):

    def __init__(self):

        Tkinter.Tk.__init__(self)

        '''Variable Setup'''
        self.hour = Tkinter.StringVar()
        self.minutes = Tkinter.StringVar()
        self.seconds = Tkinter.StringVar()

        '''Setting Up Time Inputes'''
        hourLabel = Tkinter.Label(self, text="Hours: ").pack()
        hourEntry = Tkinter.Entry(self, width=5,textvariable=self.hour).pack()

        minLabel = Tkinter.Label(self, text="Minutes: ").pack()
        minEntry = Tkinter.Entry(self, width=5,textvariable=self.minutes).pack()

        secsLabel = Tkinter.Label(self, text="Seconds: ").pack()
        secsEntry = Tkinter.Entry(self, width=5,textvariable=self.seconds).pack()

        self.timerLab = Tkinter.Label(self, text="", width=10).pack()

        startBut = Tkinter.Button(self, text="Start Timing", command=self.validation).pack()

    def validation(self):

        '''Simple Try to turn it to int value method to validate'''
        hours = self.hour.get()
        minutes = self.hour.get()
        seconds = self.seconds.get()

        try:
            hours = int(hours)
        except ValueError:
            print("Invalid Inputs")

        try:
            minutes = int(minutes)
        except ValueError:
            print("Invalid Inputs")

        try:
            seconds = int(seconds)
        except ValueError:
            print("Invalid Inputs")

        self.timer_init

    def timer_init(self):

        hours = self.hour.get()
        minutes = self.hour.get()
        seconds = self.seconds.get()

        if hours != 0:
            hoursToSeconds = int(hours)*60*60
        else:
            hoursToSeconds = 0
        if minutes != 0:
            minutesToSeconds = int(minutes)*60
        else:
            minutesToSeconds = 0

        self.totalTime = hoursToSeconds + minutesToSeconds + seconds
        self.remaining = 0
        self.countdown(10)


    def countdown(self, remaining = None):
        if remaining is not None:
            self.remaining = remaining

        if self.remaining <= 0:
            self.timerLab.configure(text="Time's Up")
        else:
            self.timerLab.configure(text="%d" % self.remaining)
            self.remaining = self.remaining - 1
            self.after(1000, self.countdown)


if __name__ == "__main__":
    app = Timer()
    app.mainloop()

Upvotes: 0

Views: 131

Answers (1)

awesoon
awesoon

Reputation: 33691

You have a lot of typos

  1. minutes = self.hour.get() should be minutes = self.minutes.get()
  2. self.timer_init - forgotten parentheses
  3. self.countdown(10) - you, probably, want self.countdown(self.totalTime) or something like that
  4. self.totalTime = hoursToSeconds + minutesToSeconds + seconds - you should convert seconds to type int

But you also have a really mistake:

self.timerLab = Tkinter.Label(self, text="", width=10).pack()

pack() method returns None. So, you have to change it to

self.timerLab = Tkinter.Label(self, text="", width=10)
self.timerLab.pack()

Upvotes: 3

Related Questions