1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import random
import numpy as np
def create_fig():
fig = plt.figure()
ax = fig.add_subplot(111)
xs = np.arange(0,10,1)
ys = np.array([random.random() for x in xs])
ax.scatter(xs, ys, color='red')
return fig
class MainApp(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master, *args, **kwargs)
self.master = master
self._initialize()
def _initialize(self):
# Place a figure
fig = create_fig()
self.fig = fig
self.canvas = FigureCanvasTkAgg(self.fig, master=self)
self.canvas.get_tk_widget().config(height=400)
self.canvas.get_tk_widget().pack(fill='both', expand=1, padx=1, pady=1)
self.canvas.draw() # <-- No impact when commented
# Place a button
b = tk.Button(self, text="START", command=self.run)
b.pack(fill='both', expand=1, padx=1, pady=1)
def run(self):
self.fig.clear()
self.fig = create_fig()
self.canvas.draw()
if __name__ == '__main__':
root = tk.Tk()
root.resizable(0,0)
MainApp(root).pack()
root.mainloop() |
Partager