我正在尝试在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)
当我运行代码时,出现以下错误:
最佳答案
这是因为 tkinter 也有一个“Button”类,并且您的通配符导入(from tkinter import *
)用 tkinter 的 Button 覆盖了您想要的 Button。这是一个典型的例子,说明为什么你永远不应该使用通配符导入。请改用 import tkinter as tk
。
关于python - 无法将按钮添加到 pyplot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133560/