import tkinter as tk
import tkinter.font
import threading
import time
def funct1():
i = 0
while True:
i+=1
time.sleep(5)
def funct2():
i = 0
while True:
i+=2
time.sleep(5)
label2.config(text=i)
def funct3():
i = 0
while True:
time.sleep(5)
i+=2
label3.config(text=i)
thread1 = threading.Thread(target=funct1)
thread2 = threading.Thread(target=funct2)
thread3 = threading.Thread(target=funct3)
thread1.start()
mainwindow = tk.Tk()
HEIGHT = 700
WIDTH = 800
canvas = tk.Canvas(mainwindow, height = HEIGHT, width = WIDTH)
canvas.pack()
frame = tk.Frame(mainwindow, bg='#08030D') #inside box
frame.place(relx=0, rely=0.1, relwidth = 0.95, relheight = 0.6)
start2=tk.Button(frame, text = "to Start 2", bg='#292230',fg='white',command = thread2.start)
start2.place(relx=0, rely=0.06, relwidth = 0.2, relheight = 0.05)
label2 = tk.Label(frame,text = "state", bg='gray')
label2.place(relx=0.45, rely=0.12, relwidth = 0.07, relheight = 0.05)
start3=tk.Button(frame, text = "to Start 3", bg='#292230',fg='white',command = thread3.start)
start3.place(relx=0, rely=0.12, relwidth = 0.2, relheight = 0.05)
label3 = tk.Label(frame,text = "state ", bg='gray')
label3.place(relx=0.45, rely=0.18, relwidth = 0.07, relheight = 0.05)
mainwindow.mainloop()
Currently I have 3 functions running as threads, with two of them only running if I press the buttons. Now if I scale that code up by
- adding more threads and functions
- create more labels and buttons
- increase sizes of functions The Tkinter window would freeze up and "lag". I have already implemented Threading as shown from online sources, but not sure if I am doing it in the most effective way regarding the GUI.