Python Forum
[Tkinter] Updating tkinter text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Updating tkinter text
#1
Is there any possible way to update the canvas text with the tkinter canvas? I have a variable with the number that has to keep updating and a variable with the text, including the number. I have a command that automatically updates the number upon the click of a button. How do I make the canvas text updating?
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#2
First, why are you using a Canvas? If you have to have it on a canvas, use create_window to put a Label on the canvas and update it.
Reply
#3
A quick example updating canvas text

import tkinter as tk

import time

def get_time(arg, canvas, var):
    show = time.strftime(f'%I:%M:%S %P')
    canvas.itemconfig(var, text=show)
    arg.after(1000, lambda: get_time(arg, canvas, var))

root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=500, bg='antiquewhite')
show = time.strftime(f'%I:%M:%S %P')
var = canvas.create_text(300,100, text=show, fill='blue', font=('serif', 20, 'bold'))
canvas.pack()

root.after(1000, lambda: get_time(root, canvas, var))
root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
It works also like this:
import tkinter as tk

import time

def get_time(canvas):
    show = time.strftime(f'%I:%M:%S %P')
    canvas.itemconfig(var, text=show)
    canvas.after(1000, get_time, canvas)

root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=500, bg='antiquewhite')
show = time.strftime(f'%I:%M:%S %P')
var = canvas.create_text(300,100, text=show, fill='blue', font=('serif', 20, 'bold'))
canvas.pack()

get_time(canvas)

root.mainloop()
Greetings wuf Smile
menator01 likes this post
Reply
#5
(Nov-24-2022, 07:22 PM)wuf Wrote: It works also like this:
import tkinter as tk

import time

def get_time(canvas):
    show = time.strftime(f'%I:%M:%S %P')
    canvas.itemconfig(var, text=show)
    canvas.after(1000, get_time, canvas)

root = tk.Tk()

canvas = tk.Canvas(root, width=600, height=500, bg='antiquewhite')
show = time.strftime(f'%I:%M:%S %P')
var = canvas.create_text(300,100, text=show, fill='blue', font=('serif', 20, 'bold'))
canvas.pack()

get_time(canvas)

root.mainloop()
Greetings wuf Smile
I tried running the code.
Error:
Traceback (most recent call last): File "C:\Users\Wannes\canvas update.py", line 13, in <module> show = time.strftime(f'%I:%M:%S %P') ValueError: Invalid format string
The only stupid person in the world, is the person that doesn't ask questions.
-Someone smart
Reply
#6
A couple of problems. First off, the call is wrong. You need to pass a time string and a format string.
Quote:classmethod datetime.strptime(date_string, format)
Return a datetime corresponding to date_string, parsed according to format.
Once that is fixed, there is still an error in the format string. Here are all the different codes you can use in the format string.

https://docs.python.org/3/library/dateti...e-behavior
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,054 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,672 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 3,759 Sep-17-2021, 06:19 AM
Last Post: zazas321
  Updating button text based upon different variable values knoxvilles_joker 0 2,211 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  tkinter text widget word wrap position chrisdb 6 7,462 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,546 Mar-10-2021, 04:21 PM
Last Post: Sir
Photo Tkinter TEXT background image _ShevaKadu 5 7,656 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 3,317 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Text Upload LearningLittlebyLittle 0 2,014 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  [Tkinter] updating tkinter chart from within function mikisDW 1 1,893 Jul-02-2020, 03:33 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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