Python Forum
repeated method call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
repeated method call
#1
Hello!
Have a nice day, everyone!
There was a problem: previously did
change of time by functions, without classes - everything worked well. I decided to make classes and I don’t know how to use the root.after function to display the current time correctly.
This is part of my code, but everything is clear from it.

import tkinter as tk
from tkinter import*
from time import strftime 

root = tk.Tk()
root.geometry('200x200+20+20')

class StartWindow: #start page loading

    def __init__(self, master):        
        self.canvas1 = Canvas(master, width=200, height=200)
        self.canvas1.place(x=0, y=0)
        game = TopWindow(self.canvas1)


class TopWindow: #display top window
    
    def __init__(self, canvas1):
        self.canvas1 = canvas1
        self.time_value = self.canvas1.create_text(50, 25, text='time')        
        time1 = Set_time(self.canvas1, self.time_value)


class Set_time: #current time change
    
    def __init__(self, canvas1, time_value):
        self.canvas1    = canvas1
        self.time_value = time_value
        self.time_2     = strftime('%H:%M:%S')
        self.canvas1.itemconfig(self.time_value,  text = self.time_2)        
        #root.after(100, self.canvas1, self.time_value) ?????????????????????


st = StartWindow(root)
root.mainloop()
Reply
#2
Why Set_time would be a class?????

import tkinter as tk
from time import strftime 
 
root = tk.Tk()
root.geometry('200x200+20+20')
 
class StartWindow: #start page loading
 
    def __init__(self, master):
        self.master = master        
        self.canvas = tk.Canvas(master, width=200, height=200)
        self.canvas.place(x=0, y=0)
        self.time_value = self.canvas.create_text(50, 25, text='time')        
        self.set_time()
     
    def set_time(self):
        self.canvas.itemconfig(self.time_value, text=strftime('%H:%M:%S'))        
        self.master.after(100, self.set_time)
 
 
st = StartWindow(root)
root.mainloop()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks! You did very well, but I wanted to do it in another class - many parameters will change and therefore I would like to use a different class for this.
Reply
#4
Then create a class for the clock
import tkinter as tk
from time import strftime 
 
root = tk.Tk()
root.geometry('200x200+20+20')
 
class StartWindow: #start page loading
    def __init__(self, root):
        self.root = root        
        self.canvas = tk.Canvas(root, width=200, height=200)
        self.canvas.place(x=0, y=0)
        self.clock = Clock(master=self, x=50, y=25, text='time')
     
class Clock:
    def __init__(self, master, x, y, text=''):
        self.master = master
        self.clock = self.master.canvas.create_text(x, y, text=text)
        self.set_time()

    def set_time(self):
        self.master.canvas.itemconfig(self.clock, text = strftime('%H:%M:%S'))        
        self.master.root.after(100, self.set_time)

st = StartWindow(root)
root.mainloop()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Thanks again !!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Call a class method from another (Tk GUI) class? Marbelous 3 6,207 Jan-15-2020, 06:55 PM
Last Post: Marbelous

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020