<p>这段代码实现了我的要求。感谢所有回复的人:</p>
<pre><code>import tkinter as tk
import matplotlib
from matplotlib import style
style.use('seaborn-darkgrid')
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import random
class GUIplot(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
printButton = tk.Button(self, text="Plot1", command=lambda: self.printMessage(canvas,a))
printButton.grid(row=0, column=0, sticky='w')
f = Figure()
f.subplots_adjust(left=0.03, bottom=0.07, right=0.98, top=0.97, wspace=0, hspace=0)
a = f.add_subplot(111, axisbg='r')
canvas = FigureCanvasTkAgg(f, self)
canvas.get_tk_widget().grid(row=1, columnspan=2)
#canvas._tkcanvas.config(bg='blue')
canvas.show()
canvas.blit()
def printMessage(self,canvas,a):
a.clear()
my_random_x = random.sample(range(100),10)
my_random_y = random.sample(range(100),10)
a.plot(my_random_x,my_random_y,'*')
canvas.draw()
print("Wow")
GUIplot1 = GUIplot()
GUIplot1.title('PlotFunction')
w, h = GUIplot1.winfo_screenwidth(), GUIplot1.winfo_screenheight()
GUIplot1.geometry("%dx%d+0+0" % (w, h))
GUIplot1.mainloop()
</code></pre>