我最近開發了一個Python程式,它可以讀取csv檔案,并在處理后回傳包含圖形的pdf檔案。但是,如果csv檔案很大,程式就會凍結,直到處理完畢。使用這種方法。鏈接,程式不再凍結,但無需按任何按鈕就會自動啟動。
下面是代碼:
try。
import Tkinter as tk, time, threading, random, Queue as queue
except ModuleNotFoundError: # Python 3
import tkinter as tk, time, threading, random, queue
class GuiPart(object)。
def __init__(self, master, queue)。
self.queue = queue
self.button1 = tk.Button(master, text="Command", padx=10,
pady=5, fg="white", bg="#263D42", command=ThreadedClient.worker_thread1)
self.button1.pack()
def processIncoming(self)。
while self.queue.qsize()。
pass: pass
class ThreadedClient(object)。
""
啟動GUI的主要部分和作業執行緒。 periodic_call()
和end_application()可以存在于GUI部分,但把它們放在
意味著你將所有的執行緒控制放在一個地方。
"""
def __init__(self, master)。
""
啟動GUI和異步執行緒。 我們在應用程式的主
(原始)執行緒中,該執行緒以后也將被
GUI也會使用。 我們為作業者(I/O)生成一個新執行緒。
""
self.master = master
# 創建佇列。
self.queue = queue.Queue()
#設定GUI部分
self.gui = GuiPart(master, self.queue)
# 設定執行緒,以進行異步I/O。
# 必要時還可以創建和使用更多的執行緒。
self.running = True
self.thread1 = threading.Thread(target=self.worker_thread1)
self.thread1.start()
# 啟動GUI中的定期呼叫,以檢查佇列。
self.periodic_call()
def periodic_call(self)。
"" 每200ms檢查一次佇列中是否有新的東西。""
self.master.after(200, self.periodic_call)
self.gui.processIncoming()
if not self.running:
# This is the brutal stop of the system. 你可能想做的是
# 在真正關閉它之前進行一些清理作業。
import sys
sys.exit(1)
def worker_thread1(self)。
""
這就是我們處理異步I/O的地方。 例如,它可能是
一個'select()'。 要記住的一件事是,執行緒必須
控制權,不管是通過選擇還是其他方式。
"""
while self.running:
# 為了模擬異步I/O,隨機創建一個亂數。
# 間隔。用真實的東西替換下面兩行。
time.sleep(rand.random() * 1.5)
filenames = filedialog.askopenfilenames(initialdir="/", title="選擇檔案", filetypes = (("comma separated file","*. csv"), ("所有檔案", "*.*")) #要求用戶選擇檔案。
"" 基于csv檔案中的資料,我正在使用matplotlib繪制一些圖表,然后我把它們匯出為pdf ""。
def end_application(self)。
self.running = False # Stops worker_thread1(由 "Done "按鈕呼叫)。
rand = random.Random()
root = tk.Tk()
client = ThreadedClient(root)
root.mainloop()
如果有人能幫助我通過點擊按鈕開始處理,我將不勝感激,因為我想添加更多的按鈕,呼叫更多的函式。
uj5u.com熱心網友回復:
執行緒立即啟動的原因是它在__init__中,所以在ThreadedClient被實體化時被呼叫。
現在你需要一種方法來啟動執行緒,所以我添加了一個叫start_thread1的函式。
你需要在你已經創建的ThreadedClient的實體中啟動執行緒。目前,你創建了一個新的實體,但這是行不通的,因為你想使用你已經有的實體(client)。因此,你需要在GuiPart中傳遞給它一個參考。我把它稱為client_instance。
然后你可以使用client_instance.start_thread1命令啟動執行緒。程式的其他部分則沒有變化。
class GuiPart(object)。
def __init__(self, master, queue, client_instance)。
self.queue = queue
self.button1 = tk.Button(master, text="Command", padx=10,
pady=5, fg="white", bg="#263D42", command=client_instance.start_thread1)
self.button1.pack()
def processIncoming(self)。
while self.queue.qsize()。
pass: pass
class ThreadedClient(object)。
""
啟動GUI的主要部分和作業執行緒。 periodic_call()
和end_application()可以存在于GUI部分,但把它們放在
意味著你將所有的執行緒控制放在一個地方。
"""
def __init__(self, master)。
""
啟動GUI和異步執行緒。 我們在應用程式的主
(原始)執行緒中,該執行緒以后也將被
GUI也會使用。 我們為作業者(I/O)生成一個新執行緒。
""
self.master = master
# 創建佇列。
self.queue = queue.Queue()
self.running = True
# 設定GUI部分 # 設定GUI部分
self.gui = GuiPart(master, self.queue, self)
# 在GUI中啟動定期呼叫,以檢查佇列。
self.periodic_call()
def start_thread1(self)。
# Set up the thread to do asynchronous I/O.
# 如果需要,還可以創建和使用更多的執行緒。
thread1 = threading.Thread(target=self.worker_thread1)
thread1.start()
uj5u.com熱心網友回復:
你可以嘗試用這種方式來打開檔案盒功能,然后你可以逐個應用或添加其他的功能,這是為了舉例說明
。from tkinter import *
from tkinter import filedialog, Text
from tkinter import messagebox
import os
root = Tk()
root.geometry('800x400')
root.title('FILE OPENER')
obj=(root)
def openbox() 。
# for i in range(5):.
"""我使用for回圈來自動打開5次,如果你不想要自動打開,那么就使用哈希值。
要自動的話,只需用哈希值注釋掉for回圈函式和
縮進"""
fileOpen = filedialog.askopenfilename(initialdir="C:/", title="File Finder",
filetypes=(("可執行", ".exe"),
("所有檔案", "*.*"))
messagebox.showinfo("Thanks for Your Patience", "You HAVE PATIENCE")
my_Btn = Button(root, text= "CLICK ME", command=openbox).pack()
root.mainloop()
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/319443.html
標籤:
