无法向pyplot添加按钮?

时间:2017-07-16 21:45:00

标签: python matplotlib tkinter

我正在尝试在pyplot中向我的窗口添加一个按钮,就像在matplotlib.widgets的文档中给出的示例,我不能发布孔代码(SO的限制),但我认为问题是标题

import matplotlib

import numpy as np
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, RadioButtons, Button

import time
import random
import sys
from tkinter import *

import matplotlib.animation as animation
import matplotlib.pyplot as plt

按钮实例化如下:

class Index(object):
    ind = 0

    def next(self, event):
        self.ind += 1
        print("just pressed next")

callback = Index()
axprev = plt.axes([0.7, 0.05, 0.1, 0.075])
axnext = plt.axes([0.81, 0.05, 0.1, 0.075])
bnext = Button(axnext, 'Next')
bnext.on_clicked(callback.next)
bprev = Button(axprev, 'Previous')
bprev.on_clicked(callback.next)

当我运行代码时,出现以下错误: error message

1 个答案:

答案 0 :(得分:5)

这是因为tkinter还有一个“Button”类,你的通配符导入(from tkinter import *)用tkinter的Button覆盖了你想要的Button。这是为什么您永远不应该使用通配符导入的典型示例。请改用import tkinter as tk