我有一个用Wxpython创建的GUI。在我的主GUI中,我显示和隐藏了一些面板。我也将我的Panel类中的一个函数称为“活动”,每次使用Main类中的新数据调用该函数时,都应使用Cartopy贴图和一些坐标来重绘Matplot。
我的问题是画布未按预期重绘。我用self.ax.clear()
清除了图,用self.fig.canvas.draw_idle()
重绘了图。但是在第一次从主菜单调用“活动”之后,画布每次都会显示相同的地图。
那我该如何正确重绘地图?
传递给面板类的参数“ d”工作正常,并且按预期进行了更改。如果我打印数据库中的所有数据,也可以正确显示。
这是我的简化代码:
def OnTest(self, d):
self.panel_one.Hide()
self.panel_two.Hide()
self.panel_three.Hide()
self.panel_four.Hide()
RedrawTest.PanelOne.active(self.panel_five, d)
self.panel_five.Show()
self.Layout()
这里是面板类:
import wx
import wx.adv
from wx import TextCtrl, EVT_TEXT_ENTER, TE_PROCESS_ENTER
import matplotlib
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
from matplotlib.offsetbox import AnnotationBbox,OffsetImage
import cartopy
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER
import sqlite3
class PanelOne(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent=parent)
mainsizer = wx.BoxSizer(wx.VERTICAL)
self.fig=plt.figure(figsize=(100, 20))
self.canvas = FigureCanvas(self, 1, self.fig)
mainsizer = wx.BoxSizer(wx.VERTICAL)
mainsizer.Add(self.canvas, 1)
self.SetSizer(mainsizer)
self.Layout()
self.Show()
def active(self, d):
conn = sqlite3.connect("Database.db")
c = conn.cursor()
c.execute('SELECT Coordinates from Test WHERE TestID = ?', (d,))
coordinates = c.fetchone()[0]
item_coord = coordinates.split(',')
location_lat, location_lon = float(item_coord[0]),float(item_coord[1])
self.ax = plt.axes(projection=ccrs.PlateCarree())
self.ax.clear()
self.ax.add_feature(cartopy.feature.LAND, facecolor='#FAFAFA')
self.ax.add_feature(cartopy.feature.OCEAN, facecolor='#F0F0F0')
self.ax.add_feature(cartopy.feature.COASTLINE, alpha=.25)
self.ax.add_feature(cartopy.feature.BORDERS, linestyle='-', alpha=.25)
self.ax.set_extent([-150, 60, -25, 90])
self.fig.tight_layout()
self.ax.set_facecolor('#F0F0F0')
self.ax.plot([location_lon], [location_lat],
color='green', linewidth=2, marker='o',
transform=ccrs.Geodetic(),
)
self.ax.text(location_lon + 2, location_lat - 2, str(coordinates),
horizontalalignment='left', fontsize=16,
transform=ccrs.Geodetic())
plt.gca().outline_patch.set_visible(False)
self.fig.canvas.draw_idle()
plt.close()