python3 下 画图 程序 bug
操作系统 win 10
错误日志:
Exception ignored in: <function Image.del at 0x076BE6A8>
Traceback (most recent call last):
File “C:\Python37\lib\tkinter_init_.py”, line 3508, in del
self.tk.call(‘image’, ‘delete’, self.name)
RuntimeError: main thread is not in main loop
Tcl_AsyncDelete: async handler deleted by the wrong thread
python 代码 :
import matplotlib
#matplotlib.use('Agg')
from matplotlib import pyplot as plt
#首先定义两个函数(正弦&余弦)
import numpy as np
import threading
from tkinter import *
class StockGui:
def __init__(self):
self.window = Tk()
self.window.title("windows")
self.window.resizable(500, 500)
frame1 = Frame(self.window)
frame1.pack(padx=10, pady=10)
self.g_run= Button(frame1, text="test",command=self.displayChart)
self.g_run.pack(side=LEFT)
self.window.mainloop()
def displayChart(self):
X=np.linspace(-np.pi,np.pi,256,endpoint=True)#-π to+π的256个值
C,S=np.cos(X),np.sin(X)
plt.plot(X,C)
plt.plot(X,S)
#在ipython的交互环境中需要这句话才能显示出来
plt.show()
if __name__ == '__main__':
t1 = threading.Thread(target=StockGui)
t1.start()
主要实现在gui 界面中点击显示图形。
尝试过加入 matplotlib.use(‘Agg’) ,图形不显示。求大神帮解。