还有很多与此问题类似的问题,但是我仍然不知道如何将图形固定到可滚动画布上。我的想法是,我只希望这些图只出现在前一个图旁边的可滚动画布上。此时,当我创建另一个图形时,它将仅绘制在画布外部。请注意,我只是将scrollregion设置为一个随机的高数字。实际上,它需要随着添加的每个额外图形而增长。但是,我不知道如何。
有人知道我在这里做错了吗?
import tkinter as tk
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class Example:
def __init__(self, master):
self.clicks = 0
self.master = master
self.frame= tk.Frame(master,width=600,height=100,background="green")
self.canv = tk.Canvas(master, width=600, height=400,background="blue",scrollregion=(0,0,2000,2000))
button = tk.Button(master,text='click',command= lambda: self.select(master))
self.scrollY = tk.Scrollbar(master, orient=tk.VERTICAL,
command=self.canv.yview)
self.scrollX = tk.Scrollbar(master, orient=tk.HORIZONTAL,
command=self.canv.xview)
self.canv['xscrollcommand'] = self.scrollX.set
self.canv['yscrollcommand'] = self.scrollY.set
self.frame.grid(row=0,column=0,rowspan=3)
self.scrollY.grid(row=3, column=1, sticky=tk.N+tk.S)
button.grid(row=2,column=0,padx=10,sticky=tk.NW)
self.scrollX.grid(row=4, column=0, sticky=tk.E+tk.W)
self.canv.grid(row=3, column=0)
def select(self,master):
self.clicks +=1
shape = np.random.randint(0,2,[5,5])
lon = np.arange(5)
lat = np.arange(5)
fig = Figure(figsize=(4,4))
ax = fig.add_subplot(111)
c = ax.pcolor(lon,lat,shape)
fig.colorbar(c,ax=ax,fraction=0.046,pad=0.04)
canvas = FigureCanvasTkAgg(fig,self.canv)
canvas.get_tk_widget().grid(row=0,column=self.clicks)
if __name__ == "__main__":
root=tk.Tk()
Example(root)
root.mainloop()