Sam.Natale
Sam.Natale

Reputation: 50

How to use tick() in a class [Python 3.x]

So basically I am doing a school project and it involves classes and tkinter, I want to put my tick() function into the class so that I can have a clock in my program, however I can't seem to get it to work when I add it to a class?

Any help/advice would be appreciated.

import urllib.request
from tkinter import *
from tkinter import ttk
import time


class MainWindow(object):
    def __init__(self):
        self.root = Tk()
        self.root.state("zoomed")  #to make it full screen
        self.root.title("Vehicle Window Fitting - Management System")
        self.root.configure(bg="grey80")

        self.frame_1 = Frame(self.root, width=1350, height=50)
        self.frame_1.pack(side=TOP, fill=X, expand=1, anchor=N)

        self.title_lbl = Label(self.frame_1, font=('arial', 12, 'bold'), text="Vehicle Window Fitting - Management System", bd=5, anchor=W)
        self.title_lbl.pack(side=LEFT) #puts title on left side

        self.clockFrame = Frame(self.frame_1, width=100, height=50, bd=4, relief="ridge")
        self.clockFrame.pack(side=RIGHT)#puts clock frame on right
        self.clockLabel = Label(self.clockFrame, font=('arial', 12, 'bold'), bd=5, anchor=E)
        self.clockLabel.pack() #puts the number in the clock frame

    def tick(self, curtime=''):  #acts as a clock, changing the label when the time goes up
        newtime = time.strftime('%H:%M:%S')
        if newtime != curtime:
            curtime = newtime
            self.clockLabel.config(text=curtime)
        self.clockLabel.after(200, tick, curtime)

(more code here but irrelevant) then:

window = MainWindow()
clock = window.tick()

Everything is in place correctly but the clock won't change it is stationary. I have managed to get it working outside of a class but how do I implement it into a class?

Upvotes: 1

Views: 823

Answers (1)

Sam.Natale
Sam.Natale

Reputation: 50

fixed by changing

self.clockLabel.after(200, tick, curtime)

to

self.clockLabel.after(200, self.tick, curtime)

took me a long time to figure that one out haha

Upvotes: 1

Related Questions