0
我无法修改此代码,因此可以通过单击图例在tkinter中选择一行。我想修改这个代码exampleMatplotlib/tkinter:在图例中挑选事件
这是我到目前为止的代码。它只是在tkinter绘制线。
import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Main:
def __init__(self, master):
self.master = master
master.title("A simple GUI")
plotFrame = tk.Frame(master)
plotFrame.pack()
f = Figure(figsize=(5,4),dpi=100)
self.ax = f.add_subplot(111)
self.canvas = FigureCanvasTkAgg(f,master=plotFrame)
self.canvas.show()
self.canvas.get_tk_widget().pack()
line1, = self.ax.plot(t, y1, lw=2, color='red', label='1 HZ')
line2, = self.ax.plot(t, y2, lw=2, color='blue', label='2 HZ')
leg = self.ax.legend(loc='upper left', fancybox=True, shadow=True)
leg.get_frame().set_alpha(0.4)
def onpick(event):
# on the pick event, find the orig line corresponding to the
# legend proxy line, and toggle the visibility
legline = event.artist
origline = lined[legline]
vis = not origline.get_visible()
origline.set_visible(vis)
# Change the alpha on the line in the legend so we can see what lines
# have been toggled
if vis:
legline.set_alpha(1.0)
else:
legline.set_alpha(0.2)
fig.canvas.draw()
fig.canvas.mpl_connect('pick_event', onpick)
plt.show()
root = tk.Tk()
my_gui = Main(root)
root.mainloop()
我不明白这个问题。在你链接的例子中,有一个函数'on_pick'和一个'pick_event''注册到它。由于这些部分在代码中完全缺失,所以它当然不会显示所需的效果。您需要包含它们,如果您遇到问题,请显示重现此问题的代码。 – ImportanceOfBeingErnest
我编辑了代码以显示我会做什么。如果代码是从Python运行的,则可以打开和关闭这些行。如果我运行这个,不能在tkinter窗口中打开或关闭这些行。 –
注意你的缩进。还有> 5个未定义的变量。在问题中发布代码之后,请验证它是否可以被复制并运行以重现所询问的(不)期望的行为。 – ImportanceOfBeingErnest