簡體   English   中英

Tkinter Gui 有時會在運行更多線程時凍結

[英]Tkinter Gui freezes sometimes as more threads are running

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()

目前我有 3 個函數作為線程運行,其中兩個只有在我按下按鈕時才會運行。 現在,如果我將代碼按比例放大

  1. 添加更多線程和功能
  2. 創建更多標簽和按鈕
  3. 增加函數的大小 Tkinter window 將凍結並“滯后”。 我已經實現了從在線資源中顯示的線程,但不確定我是否以關於 GUI 的最有效方式來執行它。

以下是如何執行類似於我在您詢問示例后在評論中向您提及的問題回答中的內容。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM