传递子图参考的行为在直接参考和列表中有所不同

时间:2019-06-25 03:31:42

标签: python matplotlib tkinter pass-by-reference

我正在尝试编写一个函数,以在选择组合框后更新matplotlib图表。我不了解行为上的差异。在完整程序和下面的简化版本中,我创建一个子图“ a”并将其传递给函数(“测试器”),它将按预期修改子图属性。但是,如果我将子图作为lambda函数中的条目传递给Comboboxselected条目,则它不起作用。为什么会有所不同?

运行以下命令时,绘图的x轴将读取“ XAXIS RESET BY TESTER”,这与调用tester()的调用一样。当选择一个新的组合框条目时,update_chart()正确地在组合框中打印选择,正确地打印附带的测试参数“ ZERO”,但是图表上的x轴不变。为什么在行为上有所不同?

以下独立代码重现了该问题:

# -*- coding: utf-8 -*-
from tkinter import ttk
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
                                               NavigationToolbar2TkAgg)
from matplotlib.figure import Figure

def make_chart_page(nb, chartPage):
    labelFrameChart = tk.LabelFrame(chartPage, text="Chart control:")
    labelFrameChart.pack(side="left", fill="y", padx=5, pady=5)
    comboChart = ttk.Combobox(labelFrameChart, values=["Foos", "Bars", "Widgets"], width=25)
    comboChart.pack(anchor="w", padx=5)
    f = Figure(figsize=(7,5), dpi=100)
    a = f.add_subplot(111)
    canvas = FigureCanvasTkAgg(f, chartPage)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
    data = [ "ZERO", a ]
    comboChart.bind("<<ComboboxSelected>>", lambda event, args=data: callback(event, args))

    tester(a)

def tester(a):
    a.set_xlabel("XAXIS RESET BY TESTER")

def update_chart(combo, var0, chart):
    print ("Combobox choice is: ", combo)
    print ("Args[0] is: ", var0)
    chart.set_xlabel("Xaxis reset by Update_Chart")

def callback(eventObject, args):
    update_chart(eventObject.widget.get(), args[0], args[1])

def demo():
    root = tk.Tk()
    nb = ttk.Notebook(root)
    chartPage = ttk.Frame(nb)

    make_chart_page(nb, chartPage)

    nb.add(chartPage, text='Charts')
    nb.pack(expand=1, fill="both")

    root.mainloop()

if __name__ == "__main__":
    demo()

谢谢你, 兰迪

1 个答案:

答案 0 :(得分:0)

您只需要通过调用canvas.draw_idle()重画画布即可。

def make_chart_page(nb, chartPage):
    ...
    global canvas
    canvas = FigureCanvasTkAgg(f, chartPage)
    ...

def update_chart(combo, var0, chart):
    print("Combobox choice is: ", combo)
    print("Args[0] is: ", var0)
    chart.set_xlabel("Xaxis reset by Update_Chart")
    canvas.draw_idle()