-1

I'm trying to make GUI program which plots pandas dataframe (tkinter, matplotlib)

but there's some problem with assigning button commands

what I want is to make one line visible/invisible when I click button,

at first I tried to assign buttons using for loop, so that I don't need to care about number of lines

but when I click button, only last line toggles state

so I break the loop and assign without loop, and checked it work.

how can I make first method works?

import numpy as np
import matplotlib.pyplot as plt
import tkinter as tk
from tkinter import ttk

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

# Sample Data
x = np.arange(100)
y1 = np.sin(x)
y2 = np.cos(x)

data = pd.DataFrame()
data['time'] = x
data['sin'] = y1
data['cos'] = y2


class graph():
    def __init__(self):
        self.win = tk.Tk()
        # Graph region
        self.frame1 = ttk.LabelFrame(self.win,text='Figure')
        self.frame1.grid(row=0,column=0,columnspan=2)

        self.fig,self.ax = plt.subplots()

        self.canvas = FigureCanvasTkAgg(self.fig,master=self.frame1)
        self.canvas.get_tk_widget().grid(row=0,column=0)
        self._read_data(data)

        # Make buttons using for loop
        # it doesn't work!!
        self.frame2= ttk.LabelFrame(self.win,text='graph handle')
        self.frame2.grid(row=1,column=0)

        for i,col in enumerate(self.pic_dic.keys()):
            ttk.Label(self.frame2,text=col).grid(row=i,column=0)
            ttk.Button(self.frame2,text='on/off Graph',command=lambda:self._onoff_pic(col)).grid(row=i,column=1)

        # Make buttons using without loop
        # but it works!!
        self.frame3= ttk.LabelFrame(self.win,text='graph handle (without for loop)')
        self.frame3.grid(row=1,column=1)
        ttk.Label(self.frame3,text='time').grid(row=0,column=0)
        ttk.Button(self.frame3,text='on/off',command=lambda:self._onoff_pic('time')).grid(row=0,column=1)
        ttk.Label(self.frame3,text='sin').grid(row=1,column=0)
        ttk.Button(self.frame3,text='on/off',command=lambda:self._onoff_pic('sin')).grid(row=1,column=1)
        ttk.Label(self.frame3,text='cos').grid(row=2,column=0)
        ttk.Button(self.frame3,text='on/off',command=lambda:self._onoff_pic('cos')).grid(row=2,column=1)

        self.canvas.draw()

    def _read_data(self,data):
        self.pic_dic = {}
        for col in data.columns:
            self.pic_dic[col] = {}
            self.pic_dic[col]['line'], = self.ax.plot(data['time'],data[col])
    def _onoff_pic(self,col):
        tmp = self.pic_dic[col]['line'].get_visible()   
        self.pic_dic[col]['line'].set_visible(not tmp)
        self.canvas.draw()        

a = graph()
a.win.mainloop()
a.win.quit()

thank!

PyJi
  • 3
  • 1

1 Answers1

0

In your code:

for i,col in enumerate(self.pic_dic.keys()):
    ttk.Label(self.frame2,text=col).grid(row=i,column=0)
    ttk.Button(self.frame2,text='on/off Graph',command=lambda:self._onoff_pic(col)).grid(row=i,column=1)

the value of col will be assigned the value of last iteration after the for loop. Therefore when the lambda is called when a button is clicked, the final col value will be used.

You need to use default value of argument in the lambda:

for i, col in enumerate(self.pic_dic.keys()):
    ttk.Label(self.frame2, text=col).grid(row=i, column=0)
    ttk.Button(self.frame2, text='on/off Graph', command=lambda c=col: self._onoff_pic(c)).grid(row=i,column=1)

The default value of argument will be initialised at the creation of lambda.

acw1668
  • 40,144
  • 5
  • 22
  • 34