Python display variable with tkinter inside a method Python display variable with tkinter inside a method tkinter tkinter

Python display variable with tkinter inside a method


Ok so Here is what I would do with your code to accomplish what you are trying to do.

First lets make sure we have the correct imports.

from tkinter import *import datetimeimport time

I created this doNothing() function as a place holder for any commands I am not currently testing.

def doNothing():    pass

Now the way you had your Clockupdate(time): function would cause the label to be added every time you click the clock button. so this is probably not what you want as it just keeps adding new labels instead of replacing the existing one. Take a look at this function. All I do in this is .config() the text to be the time argument from the function. Note you do not need to redefine the time to timelabel or anything of the sort, just use time as your varable.

def Clockupdate(time):    timerefresher.config(text = time)

I don't know if you wanted to use a class for a specific reason but I think you should just use a function here. Also try to keep your quotes consistent. I know they are interchangeable but its good practice to keep them consistent.

def secondrefesher():    newtime = ""    oldtime = datetime.datetime.now()    a = str(oldtime.hour)    b = str(oldtime.minute)    c = str(oldtime.second)    curtime = (a + ":" + b + ":" + c)    if curtime != newtime:        newtime = curtime        Clockupdate(newtime)

Note I always place commands at the end of any config of a button. It helps when managing code so you can just check the end of each line as you scroll through your code. I also changed the command for Pressme to doNothing so I could test the code. You didn't provide the code for Presstoshowtextandpic and would not be able to test the code with out that part. Remember when asking a question here to use MCVE.

root = Tk()mainlabel = Label(root, text="Hey this is working!")Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command  = doNothing)Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher)Photo = PhotoImage(file = "test.png")ShowPhoto = Label(root, image = Photo)

Here I created the time label just once and then you can call the update function all you want to change the text to current time.

timerefresher = Label(root, text = "")timerefresher.pack()mainlabel.pack()Pressme.pack()Clock.pack()root.mainloop()

Here is what I think the completed code should look like for you.

from tkinter import *import datetimeimport timedef Clockupdate(time):    timerefresher.config(text = time)def secondrefesher():    newtime = ""    oldtime = datetime.datetime.now()    a = str(oldtime.hour)    b = str(oldtime.minute)    c = str(oldtime.second)    curtime = (a + ":" + b + ":" + c)    if curtime != newtime:        newtime = curtime        Clockupdate(newtime)root = Tk()mainlabel = Label(root, text = "Hey this is working!")Pressme = Button(root, text = "Press me!", bg = "red", fg = "white", command  = Presstoshowtextandpic)Clock = Button(root, text = "Clock?", bg = "blue", fg = "white", command = secondrefesher)Photo = PhotoImage(file = "test.png")ShowPhoto = Label(root, image = Photo)timerefresher = Label(root, text = "")timerefresher.pack()mainlabel.pack()Pressme.pack()Clock.pack()root.mainloop()

Edit:

In case you are trying to create a clock that is always active and does not require you to click the button you can use .after().

Take a look at this example.

from tkinter import *import datetimeimport timeroot = Tk()mainlabel = Label(root, text = "Hey this is working!")Photo = PhotoImage(file = "test.png")timerefresher = Label(root, text = "")timerefresher.pack()status_time = ""def tick():    global status_time    time2 = time.strftime("%H:%M:%S")    if time2 != status_time:        status_time = time2        timerefresher.config(text = time2)    timerefresher.after(200, tick)tick()mainlabel.pack()root.mainloop()


You should have written an MCVE so that one can test your code, especially that things are missing (such as Presstoshowtextandpic()).

When reading the rest of your code, I think you need to read about variable classes and modify Clockupdate() as follows:

def Clockupdate(time):    timelabel = StringVar() # modified    timelabel.set(time) # added    timerefresher = Label(root, textvariable=timelabel.get()) #modified    timerefresher.pack()

P.S. You should write clock_update() instead of Clockupdate()