Hi
im writing a program and one of the things i need it to do is get today's date and display it at various times.
creating the date using 'datetime' i can do the problem is passing the created date strings to different classes
i currently have 2 classes one displays a GUI with various buttons and the date in 3 tkinter labels (one for day month and year).
the functions for the buttons and dates are within another class as they will be used with a class i've yet to write.

bellow is a simplified version of what im trying to do. where ive wrote [INSERT SOMETHING HERE] is where im stuck on what to do
i tried obj1.datetimetodayday but that just brings up seemingly random numbers and the name of the function in Class2

from Tkinter import *

class Class1:
    def __init__(self):
        obj1= Class2()
        self.root = Tk()
        self.root.title("test window")
        self.can1=Canvas(self.root, width=500, height=510, bg="black")
        self.can1.pack(expand=False, fill=X)
        self.label1 = Label(self.can1, text =obj1.datetimetodayday , bg = 'black',
                       fg= 'red',font=('Arial Black', 14))
        self.can1.create_window(20,20, window=self.label1, anchor= NW)


class Class2:
    import datetime
    def __init__(self):
        pass
    
    def datetimetodayday(self):
        tdate=datetime.datetime.now()
        tdateday = str(tdate.day)
        if len(tdateday)==1:
            tdateday= "0"+tdateday

gui=Class1()
gui.root.mainloop()

oops
in the code where i wrote obj1.datetimetodayday its meant to say [INSERT SOMETHING HERE]

You don't return anything from the function datetimedtoday() so calling it doesn't return any date.

import datetime
from Tkinter import *
 
class Class1:
    def __init__(self):
        obj1= Class2()
        self.root = Tk()
        self.root.title("test window")
        self.can1=Canvas(self.root, width=500, height=510, bg="black")
        self.can1.pack(expand=False, fill=X)
        self.label1 = Label(self.can1, text =obj1.datetimetodayday() , bg = 'black',
                       fg= 'red',font=('Arial Black', 14))
        self.can1.create_window(20,20, window=self.label1, anchor= NW)
 
 
class Class2:
    def __init__(self):
        pass
 
    def datetimetodayday(self):
        tdate=datetime.datetime.now()
        tdateday = str(tdate.day)
        if len(tdateday)==1:
            tdateday= "0"+tdateday
     
        return tdateday
 
gui=Class1()
gui.root.mainloop()
This article has been dead for over six months. Start a new discussion instead.