我正在尝试做一个 GUI 来绘制图。这个想法是,当我单击“提交”时,绘图将弹出,如果我再次单击“提交”,绘图应关闭并再次打开。
但是,当我单击“提交”时,绘图显示正确,但我需要手动关闭绘图窗 Eloquent 能再次释放按钮?
我已经清理了我的代码,只包含必需的内容:
import matplotlib.pyplot as plt # import plot functions
from mpl_toolkits.mplot3d import Axes3D
from Tkinter import *
################################ TKINTER GUI ##################################
root = Tk()
label_1 = Label(root, text="File name:")
entry_1 = Entry(root)
entry_1.insert(0,"Input")
label_1.grid(sticky=E)
entry_1.grid(row=0, column=1, columnspan=3)
def saveentry():
plt.close()
name1 = entry_1.get()
################################### PLOTTING ##################################
fig = plt.figure()
ax = fig.gca(projection='3d')
plt.show(fig)
Button_1 = Button(root, text="Submit", command=saveentry)
Button_1.grid(row=7,column=0, sticky=E)
root.mainloop()
# END OF SCRIPT
我做错了什么?
最佳答案
您需要将后端从Qt4切换为Tk。您当前正在做的是从基于 Tkinter 的应用程序打开一个基于 Qt 的绘图窗口。这可以工作,但不能互操作。
在顶部某处插入以下行(不在 saveentry
函数中):
plt.switch_backend('TkAgg') # TkAgg (instead Qt4Agg)
关于python - 通过 Tkinter 关闭/打开绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49813952/