當我嘗試運行我的代碼時,一切都會執行,但after函數會立即調用update,儘管我放入了5秒的延遲。在此之上,gui沒有顯示任何圖。tkinter.after()函數沒有暫停
import tkinter as tk
import numpy as np
import time
import functools
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
main = tk.Tk()
container = tk.Frame(main)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
f = Figure(figsize=(5,5), dpi=100)
a = f.add_subplot(111)
canvas = FigureCanvasTkAgg(f, master=main)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
def update(a, canvas):
data = np.genfromtxt('test.csv', delimiter=',', names=['x', 'y'])
a.clear()
a.plot(data['x'], data['y'], color='r', label='the data')
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
print ("update")
main.after(5000, functools.partial(update, a)(canvas))
main.after(5000, functools.partial(update, a)(canvas))
main.mainloop()
任何幫助解決這個問題或只是一般的Python技巧將不勝感激。
你爲什麼使用導入時間? Time.sleep在tkinter中不起作用。 – Jake
我只是用它進行調試,忘記刪除它。 – user3517818