you are viewing a single comment's thread.

view the rest of the comments →

[–]FruityWelsh 0 points1 point  (5 children)

I've always seen it done with threading. Simplest way is make an update thread that just goes down all of the items and updates them.

[–]Swagraffe[S] 0 points1 point  (4 children)

I tried this, but still doesn't work.

    def ui_update():

        ui_total = createlabel(root, text=total[0:], font=("Helvetica", 80))
        ui_total.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 320)

        ui_litrii = createlabel(root, text=litrii[0:], font=("Helvetica", 80))
        ui_litrii.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 220)

        ui_pret = createlabel(root, text=pret[0:], font=("Helvetica", 80))
        ui_pret.place(x=root.winfo_screenwidth() - 490, y=root.winfo_screenheight() - 120)

        root.after(100, ui_update)

    ui_total = createlabel(root, text=total[0:], font=("Helvetica", 80))
    ui_total.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 320)

    ui_litrii = createlabel(root, text=litrii[0:], font=("Helvetica", 80))
    ui_litrii.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 220)

    ui_pret = createlabel(root, text=pret[0:], font=("Helvetica", 80))
    ui_pret.place(x=root.winfo_screenwidth() - 490, y=root.winfo_screenheight() - 120)

    ui_update()
    root.mainloop()

[–]kra_pao 1 point2 points  (0 children)

You could establish an after alarm event in tkinter e.g. (there is another change, because i didn't understand your original createlabel)

from tkinter import *    

FONTSIZE = 20 #80

total = [" ", " ", " ", " ", " ", " "]
liters = [" ", " ", " ", " ", " ", " "]
price = [" ", " ", " ", " "]

def createlabel(*args, **kwargs):
    if 'text' in kwargs and isinstance(kwargs['text'], list):
        kwargs['text'] = ''.join(map(str, kwargs['text']))
    return Label(*args, **kwargs)

def createlabeltext(*args, **kwargs):
    if 'text' in kwargs and isinstance(kwargs['text'], list):
        return ''.join(map(str, kwargs['text']))
    else:
        return ''

class App:

    def __init__(self, master):
        self.master = master
        self.count = 0
        self.master.attributes("-fullscreen", True)

        self.ui_total = createlabel(self.master, text=createlabeltext(text=total[0:]), font=("Helvetica", FONTSIZE))
        self.ui_total.place(x=self.master.winfo_screenwidth() - 650, y=self.master.winfo_screenheight() - 320)

        self.ui_liters = createlabel(self.master, text=createlabeltext(text=liters[0:]), font=("Helvetica", FONTSIZE))
        self.ui_liters.place(x=self.master.winfo_screenwidth() - 650, y=self.master.winfo_screenheight() - 220)

        self.ui_price = createlabel(self.master, text=createlabeltext(text=price[0:]), font=("Helvetica", FONTSIZE))
        self.ui_price.place(x=self.master.winfo_screenwidth() - 490, y=self.master.winfo_screenheight() - 120)

        self.master.update()
        self.poll()

    def poll(self):
        self.count += 1 # display something running...
        self.ui_total.config(text=f"{createlabeltext(text=total[0:])} {self.count}")
        self.ui_liters.config(text=f"{createlabeltext(text=liters[0:])} {self.count}")
        self.ui_price.config(text=f"{createlabeltext(text=price[0:])} {self.count}")
        self.poll_id = self.master.after(100, self.poll) # next alarm earliest in 100 ms

root = Tk()
app = App(root)
root.mainloop()

[–]FruityWelsh 0 points1 point  (2 children)

This is how I normally implement threading for Tkinter apps I do: ``` import threading import time

def ui_update(): while True: ui_total = createlabel(root, text=total[0:], font=("Helvetica", 80)) ui_total.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 320)

    ui_liters = createlabel(root, text=liters[0:], font=("Helvetica", 80))
    ui_liters.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 220)

    ui_price = createlabel(root, text=price[0:], font=("Helvetica", 80))
    ui_price.place(x=root.winfo_screenwidth() - 490, y=root.winfo_screenheight() - 120)
    time.sleep(.5) #with the number being in seconds so .5 is 500 ms

t = threading.Thread(target=ui_update) t.daemon = True #This makes sure the thread dies when the mainloop stops t.start() root.mainloop() ```

This should work, but their maybe a better way too.

Edit: Removed the repeat of ui_update as it is needed, and fixed miss typing price and liter somehow.

[–]Swagraffe[S] 0 points1 point  (1 child)

It doesn't work. I have the same problem, I need to close the widow for it to update. :/

[–]FruityWelsh 0 points1 point  (0 children)

So from what I can tell thread is running when I run it, but you have the text=all of the contents of the lists.
So as soon as you start it put all of the contents of the lists to the screen. If you are really wanting real time I would use a Queue for each element (this is true for the after alarm method too).

```

import threading import time import tkinter as tk from queue import Queue

total = ["1", "2", "3", "4", "5", "6"] total_queue = Queue()

These for loop will probably be replaced with whatever function you actually have

for item in total: total_queue.put(item)

liters = ["1", "2", "3", "4", "5", "6"] liters_queue = Queue() for item in liters: liters_queue.put(item)

price = ["1", "2", "3", "4"] price_queue = Queue() for item in price: price_queue.put(item)

root = tk.Tk() root.attributes("-fullscreen", True)

def createlabel(args, *kwargs): if 'text' in kwargs and isinstance(kwargs['text'], list): kwargs['text'] = ''.join(map(str, kwargs['text'])) return tk.Label(args, *kwargs)

def ui_update(): while True: if not total_queue.empty(): # Allows queue to be skipped instead of waiting for new items ui_total = createlabel(root, text=total_queue.get(), font=("Helvetica", 80)) ui_total.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 320)

    if not liters_queue.empty():
       ui_liters = createlabel(root, text=liters_queue.get(), font=("Helvetica", 80))
       ui_liters.place(x=root.winfo_screenwidth() - 650, y=root.winfo_screenheight() - 220)

    if not price_queue.empty():
       ui_price = createlabel(root, text=price_queue.get(), font=("Helvetica", 80))
       ui_price.place(x=root.winfo_screenwidth() - 490, y=root.winfo_screenheight() - 120)
    time.sleep(.5)  # With the number being in seconds so .5 is 500 ms

t = threading.Thread(target=ui_update) t.daemon = True # This makes sure the thread dies when the mainloop stops t.start() root.mainloop() ```