回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>亲爱的Python程序员们:</p>
<p>我刚刚开始使用wxpython,我认为它是一个相当酷的工具。我的问题相对简单。我尝试使用matlibplot plot函数来绘制图形wx.面板窗口(我称之为“绘图”的按钮)。当我按“绘图”时,它应该会打开一个新窗口,并在里面进行绘图。它是正确的,但是我原来的面板突然变得不活动了,我无法按下或看到任何按钮。但是,当我关闭matlibplot窗口时,我的面板再次变为活动状态,我可以创建另一个图形,但这不是我想要的方式。我希望<strong>保持</strong>所有之前的图形窗口打开并关闭它们,前提是我愿意。在</p>
<p>下面的代码看起来很长,但非常简单:您可以绘制多项式函数或exp函数。图形采用matlibplot Python样式。你可以试着运行它,看看我对非活动窗口是什么意思。在</p>
<p>有人知道我如何让主面板一直处于活动状态,只需绘制图形吗?在</p>
<p>干杯快乐的人:-)</p>
<pre><code>import wx
import numpy as np
import matplotlib.pyplot as plt
class ExamplePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
# the combobox Control
self.mytext = wx.StaticText(self, label="", pos=(20,50))
self.mytext.SetForegroundColour(wx.BLUE)
self.funcs = ["y=2x^2+3","y=exp(2x+3)"]
self.sampleList = ['polynomial', 'exponential']
self.lblhear = wx.StaticText(self, label="Function", pos=(20,20))
self.edithear = wx.ComboBox(self, pos=(100, 20), size=(95, -1), choices=self.sampleList, style=wx.CB_DROPDOWN)
self.edithear.Bind(wx.EVT_COMBOBOX, self.EvtComboBox)
# A button
self.button1 = wx.Button(self, label="Plot", pos=(20,80))
self.button1.Bind(wx.EVT_BUTTON, self.OnClick1)
def EvtComboBox(self, event):
self.data = np.linspace(0,10,100)
if event.GetString() == self.sampleList[0]:
self.mytext.SetLabel(self.funcs[0])
self.yfun=2*self.data**2+3
elif event.GetString() == self.sampleList[1]:
self.mytext.SetLabel(self.funcs[1])
self.yfun=np.exp(2*self.data+3)
else:
return()
return(self.yfun)
def OnClick1(self, event):
plt.figure(1, figsize=(7,5))
plt.plot(self.data, self.yfun, 'b-')
plt.tick_params(axis="both", labelsize=15)
plt.xlabel(r'$x$', fontsize=15)
plt.ylabel(r'$y$', fontsize=15)
plt.show()
app = wx.App(redirect=True)
frame = wx.Frame(None, title="A Simple Plotter", pos=(0, 45), size=(280,120))
ExamplePanel(frame)
frame.Show()
app.MainLoop()
</code></pre>