单击按钮显示具有不同数字的不同文本

时间:2019-09-04 06:51:44

标签: python python-3.x tkinter tkinter-canvas

此代码当前在画布上一次全部显示图形和文本。 我想在按下按钮时在第一个图形上显示文本“图形编号:1”,在第二个图形上显示文本“图形编号:2”,类似地,在第三个图形上显示文本。 这使得能够针对不同的数字显示不同的信息。 我还没弄清楚。 如何在代码中执行此操作?

该代码是可执行的。

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

all_figures1 = []
selected_figure1 = 0 


class MyClass1():

    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_figures1.append(fig)

dataPlot1 = None  

def on_window1():

    def show_figure(number):
        global dataPlot1

        current_num = str(number+1)
        total_num = str(len(all_figures1))
        counter = tk.Label(top, text = current_num + "/" + total_num)
        counter.grid(row=4,column=0)

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

        # get figure from list
        one_figure = all_figures1[number]

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

        toolbar_frame = tk.Frame(top)
        toolbar_frame.grid(row=1,column=0)
        toolbar = NavigationToolbar2Tk(dataPlot, toolbar_frame)
        toolbar.update()

    def on_prev():
        global selected_figure1

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

        show_figure(selected_figure1)

    def on_next():
        global selected_figure1

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

        show_figure(selected_figure1)

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

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

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

    canvas1 = tk.Canvas(top, width=400, height=280)
    canvas1.grid(row=0,column=200)

    for i in range(3):
        l3 = tk.Label(canvas1, text="Figure Number:" + str(i+1))
        canvas1.create_window(50,21*i, window=l3, anchor=tk.NW)

    myscrollbar= tk.Scrollbar(top,orient=tk.VERTICAL,command=canvas1.yview)
    myscrollbar.grid(row=0,column=200, sticky=tk.NS)
    canvas1.configure(yscrollcommand=myscrollbar.set)
    canvas1.bind_all('<MouseWheel>', lambda event: canvas1.yview_scroll(int(-1*(event.delta/120)), "units"))
    canvas1.configure(scrollregion=canvas1.bbox("all"))


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


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

window = tk.Tk()

mc1 = MyClass1()
mc1.generate_all_figures()

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

window.mainloop()

1 个答案:

答案 0 :(得分:0)

您的for循环一次设置所有描述,您需要创建一个全局变量来保存图形编号,然后在on_window1函数中递增该值:

figure_num=0
def on_window1():
         ...
   global figure_num
   figure_num+=1
   l3 = tk.Label(canvas1, text="Figure Number:" + str(figure_num))
   canvas1.create_window(50,21*figure_num, window=l3, anchor=tk.NW)