我无法在交互式程序中覆盖或清除画布。 它绘制了一个新图(总共2个图),但是它只是通过左键在左框架中展开,它会被覆盖,但在框架中看不到。我只想要一帧,然后再按一下按钮即可。我尝试了有关画布更新或清除的建议,但没有用
谢谢!
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from tkinter import filedialog
from tkinter import messagebox
from wrf import (to_np,interplevel, getvar, smooth2d, get_cartopy, cartopy_xlim,get_basemap,
cartopy_ylim, latlon_coords)
from netCDF4 import Dataset
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import get_cmap
import cartopy.crs as crs
from cartopy.feature import NaturalEarthFeature
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
window = tk.Tk()
window.geometry("1080x640")
window.title("Welcome to 1st App")
frame_left = tk.Frame(window,width=540,height=640)
frame_left.pack(side=tk.LEFT, fill=tk.BOTH, expand=1) #,padx=10,pady=10)
frame_right = tk.Frame(window,width=100,height=640,bg="green")
frame_right.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1) #padx=10,pady=10)
menubar = tk.Menu(window)
count = 0
timeidx = 0
def veriSec():
global path
path = filedialog.askopenfilename(title="select a file",
initialdir="/home/onurhdogan/Downloads/wrf_veri/vdfcreatedeneme")
veriSec()
def openImage():
global img_name
global count
# count += 1
# if count != 1:
# messagebox.showinfo(title="Uyari",message="Tek bir imaj secilebilir")
#else :
#Frame1()
def Frame1():
fig = Figure(figsize=(6, 6))
a = fig.add_subplot(111)
file = Dataset(str(path))
#fig = Figure(figsize=(6, 6))
#fig.clf()
#a = fig.add_subplot(111)
# Get the sea level pressure
slp = getvar(file, "slp", timeidx=0)
# Smooth the sea level pressure since it tends to be noisy near the mountains
smooth_slp = smooth2d(slp, 3, cenweight=4)
# Get the latitude and longitude points
lats, lons = latlon_coords(slp)
# Get the basemap object
bm = get_basemap(slp, ax=a, resolution='l', area_thresh=1000)
# Add geographic outlines
bm.drawcoastlines(linewidth=0.25)
bm.drawstates(linewidth=0.25)
bm.drawcountries(linewidth=0.25)
# Convert the lats and lons to x and y. Make sure you convert the lats and lons to numpy arrays via to_np, or basemap crashes with an undefined
x, y = bm(to_np(lons), to_np(lats))
# Draw the contours and filled contours
bm.contour(x, y, to_np(smooth_slp), 10, colors="black")
bm.contourf(x, y, to_np(smooth_slp), 10, cmap=get_cmap("jet"))
# plt.colorbar(shrink=.62)
a.set_title("Sea Level Pressure (hPa)")
canvas = FigureCanvasTkAgg(fig, master=frame_left)
canvas.draw()
canvas.get_tk_widget().pack(anchor=tk.NW) # side=tk.TOP) #, expand=1),fill=tk.BOTH
toolbar = NavigationToolbar2Tk(canvas, frame_left)
toolbar.update()
canvas._tkcanvas.pack(anchor=tk.NW) # side=tk.TOP) #, expand=1) fill=tk.BOTH
def button1Function():
global timeidx
global canvas
timeidx += 1
Frame1()
print(timeidx)
def button2Function():
global timeidx
if timeidx != 0:
timeidx -= 1
#openImage()
Frame1()
else:
print("First time step")
button1 = tk.Button(frame_left, text=">", activebackground="red", bg="white", fg="black", height=35, width=5,
command=button1Function)
button1.pack(side=tk.RIGHT, anchor=tk.NW) # side=tk.BOTTOM
button2 = tk.Button(frame_left, text="<", activebackground="red", bg="white", fg="black", height=35, width=5,
command=button2Function)
button2.pack(side=tk.RIGHT, anchor=tk.NW) # side=tk.BOTTOM,
menubar = tk.Menu(window)
window.config(menu = menubar)
file = tk.Menu(menubar)
menubar.add_cascade(label="File",menu=file)
file.add_command(label="Open",command=openImage)
openImage()
Frame1()
window.mainloop()