Nabil B
Nabil B

Reputation: 39

Can't add button to pyplot?

I'm trying to add a button to my window in pyplot, did like the example given in the documentation of matplotlib.widgets, I can't post the hole code (restriction of SO), but I think the problem is with the headers

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

and the button is instantiated like this :

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)

When I run the code, I get the following error : error message

Upvotes: 1

Views: 414

Answers (1)

Novel
Novel

Reputation: 13729

This is because tkinter also has a "Button" class, and your wildcard import (from tkinter import *) overwrote the Button you wanted with tkinter's Button. This is a classic example of why you should never use wildcard imports. Use import tkinter as tk instead.

Upvotes: 5

Related Questions