Home > OS >  Tkinter incremental timer
Tkinter incremental timer

Time:10-17

I would like to make this code count up by one instead of showing the time. I've tried everything but just get a ton of errors. Can someone point me in the right direction?

# importing whole module
from tkinter import *
from tkinter.ttk import *
 
# importing strftime function to
# retrieve system's time
from time import strftime
 
# creating tkinter window
root = Tk()
root.title('Clock')
 
# This function is used to
# display time on the label
def time():
    string = strftime('%H:%M:%S %p')
    lbl.config(text = string)
    lbl.after(1000, time)
 
# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font = ('calibri', 40, 'bold'),
            background = 'purple',
            foreground = 'white')
 
# Placing clock at the centre
# of the tkinter window
lbl.pack()
time()
 
mainloop()

CodePudding user response:

Simply increment an integer? You don't need the time module for that. As a guess:

# importing whole module
from tkinter import *
from tkinter.ttk import *
 
# creating tkinter window
root = Tk()
root.title('Clock')
 
# This function is used to
# display time on the label
def time(counter_var=0):
    lbl.config(text = counter_var)
    lbl.after(1000, time, counter_var   1)
 
# Styling the label widget so that clock
# will look more attractive
lbl = Label(root, font = ('calibri', 40, 'bold'),
            background = 'purple',
            foreground = 'white')
 
# Placing clock at the centre
# of the tkinter window
lbl.pack()
time()
 
mainloop()

BTW, wildcard imports (what you call "importing the whole module") are a known source of bugs and against python's official PEP8 style guide. I recommend this standard import:

import tkinter as tk
from tkinter import ttk
  • Related