Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12171

Python, tkinter: Updating a Label periodically

$
0
0

I know, this topic has been adressed in several threads, e.g. this one: Tkinter how to continuously update a label, I looked through them, but what I found did not work for me. I try to update a Label with an image into which I write a new text periodically, i.e. without any event like click, enter. I tried binding the tkinter to the update method (s. variant 1 in code) here I generate a dummy event and I tried the "after" method (variant 2 in code) but it also didn't work. Below is a minimal example. How do I manage to update this picture with text periodically?

#!/usr/bin/python# -*- coding: utf-8 -*-'''Display- Show handling hints, minimal example'''import tkinterimport osfrom PIL import Image, ImageTk, ImageDraw, ImageFontimport threading, timeimport sysroot_path = r"some_path"sys.path.append(root_path)import versionclass Display:    def __init__(self):        self.base_path_img = os.path.join(root_path, "Images/")                self.teststep_image_name = "teststep.jpg"        self.teststep_name = "teststep_init"        self.button_height = 2        self.button_width = 20        self.window_width = 900        self.teststep_height = 700        self.frame_height = 500        self.logo_height = 50        self.logo_width = 100        self.window_size = str(self.window_width) +"x400"        self.grid_columns = 3 # vertical        self.grid_rows = 3 # horizontal        self.displ = tkinter.Tk()        self.displ.maxsize = self.window_width        self.teststep_text = "TestStep init"        self.step_label_init()        self.update_time = 1        self.update_tuple = (self.teststep_image_name, self.teststep_text)        self.n_calls = 0        self.displ.after(self.update_time, self.update_img_teststep)        self.displ.mainloop()    def step_label_init(self):'''Configure Label for Teststep display with image and text'''        self.teststep_image = self.open_tk_image(self.teststep_image_name,                                                  self.window_width,                                                  self.teststep_height,                                                  self.teststep_text)        self.TestStep_Label = tkinter.Label(                                            self.displ,                                             bg = "white",                                             height = self.frame_height,                                             width = self.window_width,                                            image = self.teststep_image)        self.TestStep_Label.grid_configure(                                            row = 2,                                            column = 1,                                            columnspan = 3,                                            sticky = tkinter.NSEW)        #variant 2        #self.TestStep_Label.after(1000, self.update_img_teststep, "")    def open_tk_image(self, img_name, img_width, img_height, test_text = ""):        pil_image = Image.open(self.base_path_img + img_name, "r").resize((img_width, img_height))        img_draw = ImageDraw.Draw(pil_image)        textwidth = 20         textheight = 50        x = img_width // 4 - textwidth        y = img_height // 5 - textheight        img_draw.text((x, y), test_text, fill="yellow", font_size=30)        tk_image = ImageTk.PhotoImage(pil_image)        pil_image.close()        return tk_image    def update_img_teststep(self, input_ign):        #dummy argument        print(input_ign)        img_name = self.update_tuple[0]        teststep_text = self.update_tuple[1] +"number of calls: " + str(self.n_calls)        self.teststep_image = self.open_tk_image(img_name, self.window_width, self.teststep_height, teststep_text)        self.TestStep_Label.configure(image = self.teststep_image)        self.n_calls += 1    def displ_config(self):        #test start        for i in range(1,3):            time.sleep(self.update_time)            print(f'slept, now i is {i}')            self.update_tuple = ("teststep"+str(i)+".jpg", "this is text #" + str(i))            self.displ.configure(bg = "black")        #test end# import time# import threadingif __name__ == "__main__":    displ = Display()    #https://stackoverflow.com/questions/70355318/tkinter-how-to-continuously-update-a-label

As described above, I tried binding tkinter.Tk() to the update method and generating a dummy event that would trigger the binding function. And I tried using the method "after" with self.displ and with the self.TestStep_Label. The second variant should have solved the problem immediately, the first variant would have needed some additional waiting calls. But actually, none of both helped.


Viewing all articles
Browse latest Browse all 12171

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>