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
Here's how to do something similar to what's in my answer to the question which I referred you in a comment after you asked for an example.
import queue
import sys
import tkinter as tk
import tkinter.font
import threading
import time
WIDTH, HEIGHT = 800, 700
LABEL2_MSGID, LABEL3_MSGID = 2, 3
def funct1():
i = 0
while True:
i += 1
time.sleep(5)
def funct2():
i = 0
while True:
i += 2
time.sleep(5)
msg_queue.put((LABEL2_MSGID, i)) # Put a message in queue.
def funct3():
i = 0
while True:
time.sleep(5)
i += 2
msg_queue.put((LABEL3_MSGID, i)) # Put a message in queue.
def process_incoming():
""" Handle any messages currently in the queue. """
while msg_queue.qsize():
try:
msg = msg_queue.get_nowait()
# Check contents of message and do whatever is needed to the GUI.
msg_id, text = msg
if msg_id == LABEL2_MSGID:
label2.config(text=text)
elif msg_id == LABEL3_MSGID:
label3.config(text=text)
else:
pass # Unknown identifier.
except queue.Empty:
# Shouldn't happen, but if it does, just ignore it.
pass
def periodic_call():
""" Check every 200 ms if there is something new in the queue. """
mainwindow.after(200, periodic_call)
process_incoming()
if not running:
# This is the brutal stop of the system. You may want to do some
# cleanup before actually shutting it down.
sys.exit(1)
thread1 = threading.Thread(target=funct1, daemon=True)
thread2 = threading.Thread(target=funct2, daemon=True)
thread3 = threading.Thread(target=funct3, daemon=True)
mainwindow = tk.Tk()
msg_queue = queue.Queue() # For inter-thread communications.
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)
running = True
thread1.start() # Start background thread that doesn't affect the GUI.
periodic_call() # Start checking and processing messages from other threads.
mainwindow.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.