简体   繁体   English

如何将 tkinter 与 matplotlib checkbutton 集成以更改变量?

[英]How to get tkinter integrated with matplotlib checkbutton to change the variable?

I am trying to get tkinter check button implemented with matplotlib in order to update the plot and show the selected plots only.我正在尝试使用 matplotlib 实现 tkinter 检查按钮,以便更新绘图并仅显示选定的绘图。

However, the check button wouldn't change the variable.但是,复选按钮不会更改变量。

I've tried different versions of python and stripping down most of my code to just simple tkinter and matplotlib integration, without any luck.我尝试了不同版本的 python 并将我的大部分代码剥离为简单的 tkinter 和 matplotlib 集成,但没有任何运气。

# coding: utf-8
# usr/bin/python37

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from tkinter import messagebox, Button, Tk, BooleanVar
from tkinter.ttk import Checkbutton
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')


class GUI:
    fig = plt.figure()
    sub = fig.add_subplot(1, 1, 1)

    def __init__(self, root):
        self.root = root
        self.root.title("Testing")
        self.setupTk()

    def setupTk(self):
        self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)
        '''
        Problem
        '''
        self.var = BooleanVar()
        self.var.set(True)
        self.button = Checkbutton(
            self.root, variable=self.var, text='Direvative', command=self.cb)
        self.button.pack(side='left')
        '''
        /Problem
        '''
        self.button = Button(master=self.root, text='Quit', command=self._quit)
        self.button.pack(side='right')

        self.toolbar = NavigationToolbar2Tk(self.canvas, self.root)
        self.toolbar.update()

        self.root.protocol("WM_DELETE_WINDOW", self._quit)

    def cb(self):
        print(self.var.get())

    def _quit(self):
        if messagebox.askquestion('Exit Application', 'Are you sure you want to exit the application', icon='warning') == 'yes':
            self.root.quit()
            self.root.destroy()


if __name__ == '__main__':
    root = Tk()
    mod = GUI(root)
    root.mainloop()

I am using: Python: 3.7.3 Matplotlib: 3.1.1我正在使用: Python: 3.7.3 Matplotlib: 3.1.1

I expect the printout to change as the user clicks the check button.我希望打印输出会随着用户单击复选按钮而改变。

Please feel free to point me towards resources online.请随时为我指出在线资源。

You need to use matplotlib.Figure instead of pyplot.figure .您需要使用matplotlib.Figure而不是pyplot.figure
You also had two buttons with the same name, but that was not the problem.您还有两个同名的按钮,但这不是问题。

The following code embeds a matplotlib.Figure , with a subplot and a toolbar , in a tkinter.window that has a check_button , and a quit_button .下面的代码嵌入一个matplotlib.Figure ,有subplottoolbar ,在tkinter.window具有check_buttonquit_button I removed the messagebox that I found irritating, but you can put it back, there was nothing wrong with the code.我删除了我觉得messagebox ,但你可以把它放回去,代码没有任何问题。

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from tkinter import messagebox, Button, Tk, BooleanVar
from tkinter.ttk import Checkbutton
from matplotlib.figure import Figure
import matplotlib
matplotlib.use('TkAgg')


class GUI:

    fig = Figure()
    sub = fig.add_subplot(1, 1, 1)

    def __init__(self, root):
        self.root = root
        self.root.title("Testing")
        self.setupTk()

    def setupTk(self):

        self.var = BooleanVar()
        self.var.set(True)
        self.check_button = Checkbutton(
            self.root, variable=self.var, text='Direvative', command=self.cb)
        self.check_button.pack(side='left')

        self.quit_button = Button(master=self.root, text='Quit', command=self._quit)
        self.quit_button.pack(side='right')

        self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
        self.canvas.get_tk_widget().pack(side='top', fill='both', expand=1)
        self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)

        self.toolbar = NavigationToolbar2Tk(self.canvas, self.root)
        self.toolbar.update()

        self.root.protocol("WM_DELETE_WINDOW", self._quit)

    def cb(self):
        print(self.var.get())

    def _quit(self):
        self.root.destroy()


if __name__ == '__main__':
    root = Tk()
    mod = GUI(root)
    root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM