簡體   English   中英

如何刪除 tkinter 中的 matplot 線?

[英]How can I remove matplot lines in tkinter?

我正在嘗試制作繪制熊貓數據框的 GUI 程序(tkinter、matplotlib)

但是分配按鈕命令存在一些問題

我想要的是當我單擊按鈕時使一行可見/不可見,

起初我嘗試使用 for 循環分配按鈕,這樣我就不需要關心行數

但是當我點擊按鈕時,只有最后一行切換狀態

所以我打破了循環並在沒有循環的情況下分配,並檢查它是否有效。

我怎樣才能使第一種方法有效?

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()

謝謝!

在您的代碼中:

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)

在 for 循環之后, col的值將被分配為最后一次迭代的值。 因此,當單擊按鈕時調用 lambda 時,將使用最終的col值。

您需要在 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)

參數的默認值將在創建 lambda 時初始化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM