【问题标题】:How to get tkinter integrated with matplotlib checkbutton to change the variable?如何让 tkinter 与 matplotlib checkbutton 集成以更改变量?
【发布时间】:2019-07-15 01:34:55
【问题描述】:

我正在尝试使用 matplotlib 实现 tkinter 检查按钮,以便更新绘图并仅显示选定的绘图。

但是,检查按钮不会更改变量。

我尝试了不同版本的 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()

我正在使用: Python: 3.7.3 Matplotlib: 3.1.1

我希望当用户单击检查按钮时打印输出会发生变化。

请随时向我指出在线资源。

【问题讨论】:

    标签: python python-3.x matplotlib tkinter


    【解决方案1】:

    您需要使用matplotlib.Figure 而不是pyplot.figure
    您还有两个同名的按钮,但这不是问题。

    以下代码在具有check_buttonquit_buttontkinter.window 中嵌入了matplotlib.Figuresubplottoolbar。我删除了我觉得烦人的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()
    

    【讨论】:

    • 您缺少 matplotlib 导入,但它可以工作,谢谢!
    • 不客气。啊,是的,使用 jupyter notebook 的怪癖是存在持久性的导入和变量 - 对此感到抱歉。我编辑了代码。
    • 一直想知道为什么Figureplt.figure 更受欢迎。 +1 展示它:)
    • 是的@HenryYik,谢谢,简短的答案是here - 较长的答案there
    • @HenryYik 一般而言,plt.figure() 没有任何问题。这里的关键是要嵌入到tkinter中。在这种情况下,你不想让你的图形由 pyplot 管理,因为你想通过你正在使用的 GUI 工具包自己管理它。
    猜你喜欢
    • 2020-05-04
    • 2020-12-05
    • 2015-05-16
    • 2018-10-26
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2020-10-21
    相关资源
    最近更新 更多