Python Forum
changing tkinter label from thread
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing tkinter label from thread
#2
The thread should not be called with the method run, it should be called with the method start.
Inside the thread you have created a new instance of MainPage, the thread will need passing a reference to MainPage.
When calling the GUI from a thread you need to use the method after or the GUI will lockup.

I removed the use of keyboard as I don't have it installed.
import tkinter as tk
import time
from threading import Thread


class MainPage(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(**kwargs)
        self.state_lbl = tk.Label(self, text="initial words")
        self.state_lbl.grid(row=0, column=0)

    def changeState(self, text):
        self.state_lbl['text'] = text


class MyFunctions:

    def __init__(self, gui):
        self.gui = gui

    def one(self):
        time.sleep(5)
        print("one")
        self.gui.after(0, self.gui.changeState("updated words"))
        time.sleep(1)

    def two(self):
        print("two")
        time.sleep(1)
        self.gui.after(0, self.gui.changeState("updated words again"))


class runCycle(Thread):
    def __init__(self, gui):
        Thread.__init__(self)
        my = MyFunctions(gui)
        self.twoFunctions = [my.one, my.two]

    def run(self):
        for func in self.twoFunctions:
            func()


if __name__ == "__main__":
    root = tk.Tk()
    main = MainPage(root)
    main.grid()
    root.wm_geometry("600x300")
    run_instance = runCycle(main)
    run_instance.start()
    root.mainloop()
Take a look at the following [Tkinter] How to deal with code that blocks the mainloop, freezing the gui
Reply


Messages In This Thread
changing tkinter label from thread - by nanok66 - Jun-06-2020, 05:22 AM
RE: changing tkinter label from thread - by Yoriz - Jun-06-2020, 08:33 AM
RE: changing tkinter label from thread - by nanok66 - Jun-07-2020, 01:37 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 1,095 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,735 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,392 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 5,390 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 4,354 Sep-30-2021, 05:57 PM
Last Post: menator01
  tkinter: Image to Label Maryan 10 5,506 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Tkinter - How can I extend a label widget? TurboC 2 2,904 Oct-13-2020, 12:15 PM
Last Post: zazas321
  Tkinter: How to assign calculated value to a Label LoneStar 7 4,071 Sep-03-2020, 08:19 PM
Last Post: LoneStar
  [Tkinter] Tkinter delete values in Entries, when I'm changing the Frame robertoCarlos 11 6,127 Jul-29-2020, 07:13 PM
Last Post: deanhystad
  [Tkinter] tkinter, dropdowns with changing options Sheepykins 4 9,961 Jul-03-2020, 06:06 AM
Last Post: jdos

Forum Jump:

User Panel Messages

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