Python Forum
[Tkinter] Updating Tkinter label using multiprocessing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Updating Tkinter label using multiprocessing
#7
Are you trying to do something like this?

import multiprocessing as mp
import random as rnd
import tkinter as tk

'''
    Function for updating labels
    Creates the que
    Add to the que
    Set label text for que
    Call root.after for continuous call
'''
def get_num(root, label):
    myque = mp.Queue()
    myque.put(rnd.randint(1, 100))
    label['text'] = f'Process {myque.get()} running...'
    root.after(1000, lambda: get_num(root, label))

'''
    Tkinter window
'''
root = tk.Tk()
root.title('Running Processes')
root.columnconfigure(0, weight=1)
root.geometry('+300+300')

'''
    Two list, one to hold labelframes and one to hold labels
'''
labels = []
labelframes = []

'''
    Set variable j to for labelframe numbers
'''
j = 1

'''
    Using for loop to add 10 labels
    using j to number labelframes
    create and append labelframes to the labelframes list
'''
for i in range(10):
    j = j if i % 2 == 0 else(j if i % 3 == 0 else j+1)
    labelframes.append(tk.LabelFrame(root, text=f'Processes for frame {j}'))
    labelframes[i].grid(column=0, row=i, sticky='new', pady=5, padx=5)

    '''
    parent is used to place labels in the labelframes. Using i % n in this example
    '''
    parent = labelframes[0] if i % 2 == 0 else(labelframes[1] if i % 3 == 0 else labelframes[i])

    '''
    Create and append labels to the labels list
    Using parent to color background of grouped labels in labelframe
    '''
    labels.append(tk.Label(parent, anchor='w', padx=5, width=50))
    labels[i]['bg'] = 'sandybrown' if parent == labelframes[0] \
    else('tan' if parent == labelframes[1] else 'ivory2')
    labels[i]['relief'] = 'ridge'
    labels[i].grid(column=0, row=i, sticky='new', pady=5, padx=5)

    '''
    Create and start the process
    '''
    process = mp.Process(target=get_num, args=(root, labels[i],))
    process.start()
    process.join()

    '''
    Use root.after to call the get_num function
    '''
    root.after(1, get_num, root, labels[i])
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


Messages In This Thread
RE: Updating Tkinter label using multiprocessing - by menator01 - Aug-15-2022, 07:10 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 907 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,671 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Updating tkinter text BliepMonster 5 6,411 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 5,237 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,380 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  tkinter: Image to Label Maryan 10 5,455 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Tkinter - How can I extend a label widget? TurboC 2 2,875 Oct-13-2020, 12:15 PM
Last Post: zazas321
  Tkinter: How to assign calculated value to a Label LoneStar 7 4,019 Sep-03-2020, 08:19 PM
Last Post: LoneStar
  [Tkinter] updating tkinter chart from within function mikisDW 1 1,982 Jul-02-2020, 03:33 AM
Last Post: deanhystad
  changing tkinter label from thread nanok66 3 7,539 Jun-07-2020, 01:37 AM
Last Post: nanok66

Forum Jump:

User Panel Messages

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