【问题标题】:Display graphs in the toplevel window在顶层窗口中显示图形
【发布时间】:2019-08-29 11:52:15
【问题描述】:

我想要做什么:

我试图在顶层窗口而不是主窗口中循环显示三个显示。我收到下面提到的错误。所以,我一直无法运行它。

我得到的错误:

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Users\sel\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)

  File "<ipython-input-19-df56c3798d6a>", line 91, in on_window
    show_figure(selected_figure)

  File "<ipython-input-19-df56c3798d6a>", line 53, in show_figure
    one_figure = all_figures[number]

IndexError: list index out of range

下面是我的代码:

import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

all_figures = []
selected_figure = 0 

class MyClass():

    def __init__(self):

        self.sheets = [[1,2,3], [3,1,2], [1,5,1]]
        self.W = 2
        self.L = 5


    def plot_sheet(self, data):
        """plot single figure"""

        fig, ax = plt.subplots(1)

        ax.set_xlim([0, self.W]) 
        ax.set_ylim([0, self.L])

        ax.plot(data)

        return fig

    def generate_all_figures(self):
        """create all figures and keep them on list"""
        global all_figures

        for data in self.sheets:
            fig = self.plot_sheet(data)
            all_figures.append(fig)


dataPlot = None  


def on_window():

    def show_figure(number):
        global dataPlot

        # remove old canvas
        if dataPlot is not None: # at start there is no canvas to destroy
            dataPlot.get_tk_widget().destroy()

        # get figure from list
        one_figure = all_figures[number]

        # display canvas with figure
        dataPlot = FigureCanvasTkAgg(one_figure, master=window)
        dataPlot.draw()
        dataPlot.get_tk_widget().grid(row=0, column=0)

    def on_prev():
        global selected_figure

        # get number of previous figure
        selected_figure -= 1
        if selected_figure < 0:
            selected_figure = len(all_figures)-1

        show_figure(selected_figure)

    def on_next():
        global selected_figure

        # get number of next figure
        selected_figure += 1
        if selected_figure > len(all_figures)-1:
            selected_figure = 0

        show_figure(selected_figure)

    top = tk.Toplevel()
    top.wm_geometry("794x370")
    top.title('Optimized Map')

    selected_figure = 0
    dataPlot = None # default value for `show_figure`
    show_figure(selected_figure)

    frame = tk.Frame(top)
    frame.grid(row=1, column=0)

    b1 = tk.Button(frame, text="<<", command=on_prev)
    b1.grid(row=0, column=0)

    b2 = tk.Button(frame, text=">>", command=on_next)
    b2.grid(row=0, column=1)

window = tk.Tk()

b1 = tk.Button(window, text="Next", command=on_window)
b1.grid(row=0, column=0)

window.mainloop()

【问题讨论】:

  • 您之前没有生成数字,所以all_figures 是一个空列表,all_figures[number] 会引发 IndexError。
  • @j_4321 这里应该做什么?

标签: python tkinter


【解决方案1】:

您已经创建了一个带有generate_all_figures 的类,但您还没有创建MyClass 对象并运行generate_all_figures(),因此您的all_figures 列表为空。这就是您收到IndexError 的原因。

在执行on_window()之前,您需要创建一个MyClass对象并运行generate_all_figures()来填充您的all_figures列表:

window = tk.Tk()

mc = MyClass()
mc.generate_all_figures()

b1 = tk.Button(window, text="Next", command=on_window)
b1.grid(row=0, column=0)

window.mainloop()

顺便说一句,generate_all_figures 中不需要global all_figures(参见Defining lists as global variables in Python)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-06
    • 2019-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多