我有一个包含matplotlib画布的类A
。这个画布是可点击的,即我点击图,这会调用一个保存event.x = self.x
的函数。
我有另一个类B
,它应该在任何时候更改时从类A
接收属性。我在这篇文章How to trigger function on value change?中看到了观察者模式,我认为这可能是我需要的,但我无法让它运行(见下面的代码)。
这两个课程都坐在两个tkk框架中,但我想这与问题无关。
明确地说,在这个最小的例子中,我想将plotFrame.x
,plotFrame.y
传递给WorkFrame()
类。每次我点击图表,我都希望在右边的小标签中看到新值!
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 = self.PlotFrame(frame1, frame2)
frame3 = ttk.Frame(self.window, relief=tk.RIDGE)
frame3.grid(row=2, column=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
self.workFrame = self.WorkFrame(frame3, self.plotFrame)
class PlotFrame():
# The plot
def __init__(self, parent1, parent2):
self.parent1 = parent1
self.parent2 = parent2
self.observers = []
self.x = 0
self.y = 0
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!
self.x = event.x
self.y = event.y
self.position()
@property
def position(self):
return(self.x,self.y)
@position.setter
def position(self, x, y):
self.x = x
self.y = y
for callback in self.observers:
self.observers.append(callback)
def bind_to(self, callback):
self.observers.append(callback)
class WorkFrame():
def __init__(self, parent, plot_frame):
self.parent = parent
self.x = 0
self.y = 0
self.plot_frame = plot_frame
self.plot_frame.bind_to(self.update_position)
self.display()
def update_position(self, x, y):
self.x = x
self.y = y
def display(self):
l_x = tk.Label(self.parent, text ='Xposition: ' + str(self.x))
l_y = tk.Label(self.parent, text ='Yposition: ' + str(self.y))
l_x.grid(row = 0, column=0)
l_y.grid(row = 0, column=1)
# Create the entire GUI program
program = Frame_examples_program()
# Start the GUI event loop
program.window.mainloop()
这引发了例外:
File "test_xy_positions.py", line 65, in onclick
self.position()
TypeError: 'tuple' object is not callable
这是指我返回元组(self.x,self.y)
,position(self)
的事实。
答案 0 :(得分:1)
我正在使用python3并且已经将导入更改为与原始示例一起使用,但这对我来说非常有效,请告诉我它是如何工作的。我试图评论我改变的所有地方,但可能错过了一个:
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(object):
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 = self.PlotFrame(frame1, frame2)
frame3 = ttk.Frame(self.window, relief=tk.RIDGE)
frame3.grid(row=2, column=2, sticky=tk.E + tk.W + tk.N + tk.S, padx=0, pady=0)
self.workFrame = self.WorkFrame(frame3, self.plotFrame)
class PlotFrame(object):
# The plot
def __init__(self, parent1, parent2):
self.parent1 = parent1
self.parent2 = parent2
self.observers = []
self.x = 0
self.y = 0
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):
# Here I am now setting the position
self.set_new_position(event.x, event.y)
def set_new_position(self, x, y):
self.x = x
self.y = y
for callback in self.observers:
# Here I am now calling the methods that have been captured so far
# and passing them the arguments of x, y to do with as they please
callback(self.x, self.y)
def bind_to(self, callback):
self.observers.append(callback)
class WorkFrame():
def __init__(self, parent, plot_frame):
self.parent = parent
self.x = 0
self.y = 0
self.plot_frame = plot_frame
self.plot_frame.bind_to(self.update_position)
self.display()
def update_position(self, x, y):
self.x = x
self.y = y
# Here I have added the requirement to run the display code again
# after an update
self.display()
def display(self):
l_x = tk.Label(self.parent, text ='Xposition: ' + str(self.x))
l_y = tk.Label(self.parent, text ='Yposition: ' + str(self.y))
l_x.grid(row = 0, column=0)
l_y.grid(row = 0, column=1)
# Create the entire GUI program
program = Frame_examples_program()
# Start the GUI event loop
program.window.mainloop()
答案 1 :(得分:0)
只是从臀部拍摄,您是否尝试从位置方法上方删除@property装饰器?然后该函数将是可调用的,而不是一个不可调用的属性。