Я хотел бы иметь графический интерфейс, в котором одна часть интерфейса содержит график, а в остальной части окна - несколько инструментов для работы с графиком.
Я хотел бы использовать mpl_connect для соединения холста matplotlib с рамкой tkk, чтобы я мог выбирать точки на графике для работы.
Это была моя попытка, которая трусливо отказывается делатья думаю, что это должно сделать:
import Tkinter as tk
import ttk
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2TkAgg)
import matplotlib.pyplot as plt
import numpy as np
class Frame_examples_program():
def __init__(self):
self.window = tk.Tk()
self.window.title("Amazing GUI 5000")
self.create_widgets()
def create_widgets(self):
self.window['padx'] = 10
self.window['pady'] = 10
# - - - - - - - - - - - - - - - - - - - - -
# Frame
frame1 = ttk.Frame(self.window, relief=tk.RIDGE)
frame1.grid(row=0, column=0, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
frame2 = ttk.Frame(self.window, relief=tk.RIDGE)
frame2.grid(row=1, column=0, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
self.PlotFrame(frame1, frame2)
class PlotFrame():
# The plot
def __init__(self, parent1, parent2):
self.parent1 = parent1
self.parent2 = parent2
canvas = self.plot()
self.plot_toolbar(canvas)
def plot(self):
# the actual plot
fig, ax = plt.subplots()
plt.imshow(np.ones((100,100)),picker=True)
canvas = FigureCanvasTkAgg(fig, self.parent1)
canvas.mpl_connect('button_press_event', self.onclick)
return(canvas)
def plot_toolbar(self, canvas):
# the tool bar to the plot
toolbar = NavigationToolbar2TkAgg(canvas, self.parent2)
toolbar.update()
canvas.get_tk_widget().grid(row=1, column=1)
canvas.draw()
def onclick(self, event):
# the devilish thing that does nothing!
print('WOHOOOO')
# Create the entire GUI program
program = Frame_examples_program()
# Start the GUI event loop
program.window.mainloop()
Как вы увидите, когда вы запустите это, панель инструментов matplotlib прекрасно работает, но я просто не могу вызвать событие onclick!Почему?