从Windows移动到Linux时,WXPython接口无法正常工作

从Windows移动到Linux时,WXPython接口无法正常工作,python,linux,windows,matplotlib,wxpython,Python,Linux,Windows,Matplotlib,Wxpython,我几乎完成了图形界面的试用版。我在Windows上做了这项工作,然后转到Linux进行测试。 我遇到的问题是Matplotlib面板正在被剪切(仅在Linux版本上),因此我无法看到图形。我不能只是调整大小来解决这个问题 下面你可以看到我的代码。有什么想法吗 # -*- coding: latin-1 -*- import wx import sys import numpy import numpy as np import matplotlib import os import matpl

我几乎完成了图形界面的试用版。我在Windows上做了这项工作,然后转到Linux进行测试。 我遇到的问题是Matplotlib面板正在被剪切(仅在Linux版本上),因此我无法看到图形。我不能只是调整大小来解决这个问题

下面你可以看到我的代码。有什么想法吗

 # -*- coding: latin-1 -*-
import wx
import sys
import numpy
import numpy as np
import matplotlib
import os
import matplotlib.pyplot as plt
matplotlib.use('WXAgg')
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import \
    FigureCanvasWxAgg as FigCanvas, \
    NavigationToolbar2WxAgg as NavigationToolbar
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx

ini3=0
fin3=0
pico3=[]
nomeAbre3=''
iniespec=0
intervaloespec=400


class TestFrame(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self,parent,title=title,size=(2000,800))
        self.sp = wx.SplitterWindow(self)
        self.p1 = MainPanel(self.sp)
        self.p2 = MatplotPanel(self.sp)
        self.statusbar = self.CreateStatusBar()
        self.box = wx.BoxSizer(wx.VERTICAL)
        self.box.Add(self.p1, 1, wx.EXPAND)
        self.box.Add(self.p2, 1, wx.EXPAND)
        self.p2.draw()
        self.create_menu()

        self.SetAutoLayout(True)
        self.SetSizer(self.box)
        self.Layout()
        self.statusbar.SetStatusText('Oi')

    def create_menu(self):
        self.menubar = wx.MenuBar()

        menu_file = wx.Menu()
        m_expt = menu_file.Append(-1, "&Save plot\tCtrl-S", "Save plot to file")

        menu_file.AppendSeparator()
        m_exit = menu_file.Append(-1, "E&xit\tCtrl-X", "Exit")


        menu_help = wx.Menu()
        m_about = menu_help.Append(-1, "&About\tF1", "About the demo")


        self.menubar.Append(menu_file, "&File")
        self.menubar.Append(menu_help, "&Help")
        self.SetMenuBar(self.menubar)

class MainPanel(wx.Panel):
    def __init__(self,parent):
        """ Creates the main panel with all the controls on it:
             * mpl canvas 
             * mpl navigation toolbar
             * Control panel for interaction
        """
        self.nomeAbre = 'Spectrum'
        self.nomeFundo = 'Spectrum2'
        self.tipoDoAjuste = '1'
        self.grauDoFundo = '2'
        self.posicaoDosPicos = ''
        self.regiaoDoAjusteI = ''
        self.regiaoDoAjusteF = ''

        wx.Panel.__init__(self, parent)

        self.dpi = 100
        self.fig = Figure((10.0, 6.0), dpi=self.dpi)

        self.abreArquivo = wx.Button(self, -1, "Open:", (10,10))
        self.Bind(wx.EVT_BUTTON, self.openFile, self.abreArquivo)
        self.editname = wx.TextCtrl(self, value=self.nomeAbre, pos=(100, 10), size=(200,-1))
        self.AbreArquivoFundo = wx.Button(self, -1, "Fundo:", (10,40))
        self.editname2 = wx.TextCtrl(self, value=self.nomeFundo, pos=(100, 40), size=(200,-1))
        self.Bind(wx.EVT_BUTTON, self.openFile2, self.AbreArquivoFundo)


        self.ajusteTipo = wx.StaticText(self, -1, "A:", pos=(20, 80))
        self.ajusteTipoCaixa = wx.TextCtrl(self, value=self.tipoDoAjuste, pos=(100, 78), size=(25,-1))

        self.fundoGrau = wx.StaticText(self, -1, "B parameter:", pos=(140, 80))
        self.fundoGrauCaixa = wx.TextCtrl(self, value=self.grauDoFundo, pos=(220, 78), size=(25,-1))

        self.posicaoPicos = wx.StaticText(self, -1, "Posicao dos Picos (max. 10 valores):", pos=(20, 120))
        self.posicaoPicosCaixa = wx.TextCtrl(self, value=self.posicaoDosPicos, pos=(245, 118), size=(350,-1))

        self.regiaoAjuste = wx.StaticText(self, -1, "Regiao do Ajuste:", pos=(20, 160))
        self.regiaoAjusteI = wx.TextCtrl(self, value='', pos=(140, 158), size=(60,-1))
        self.regiaoAjusteF = wx.TextCtrl(self, value='', pos=(220, 158), size=(60,-1))

        self.inicioEspectroTexto = wx.StaticText(self, -1, "Start:", pos=(250, 302))
        self.inicioEspectro = wx.TextCtrl(self, -1, "", pos=(300, 300))
        self.Bind(wx.EVT_TEXT,self.funcInicioEspectro,self.inicioEspectro)

        self.intervaloEspectroTexto = wx.StaticText(self, -1, "Interval:", pos=(420, 302))
        self.intervaloEspectro = wx.TextCtrl(self, -1, "400", pos=(490, 300),size=(50,-1))
        self.Bind(wx.EVT_TEXT,self.funcintervaloEspectro,self.intervaloEspectro)

        self.idf = wx.Button(self, -1, "IDF", (100,300))
        self.Bind(wx.EVT_BUTTON,self.criaIDF,self.idf)

        global ini3
        global fin3
        global pico3
        global iniespec
        global intervaloespec
        self.Bind(wx.EVT_MOTION,lambda event:  self.OnMove(event,ini3,1))
        self.Bind(wx.EVT_MOTION,lambda event:  self.OnMove(event,fin3,2))
        self.Bind(wx.EVT_MOTION,lambda event:  self.OnMove(event,pico3,3))
        self.Bind(wx.EVT_MOTION,lambda event:  self.OnMove(event,iniespec,4))
        self.Bind(wx.EVT_MOTION,lambda event:  self.OnMove(event,intervaloespec,5))



    def criaIDF(self, event):
        a1 = nomeAbre3
        a2 = str(len(pico3))+','+str(ini3)+','+str(fin3)+',,'
        for j in pico3:
            a2 += str(j) + ','
        a2 += ','
        print(a1)
        print(a2)

    def funcInicioEspectro(self,event):
        global iniespec
        iniespec = self.inicioEspectro.GetValue()

    def funcintervaloEspectro(self,event):
        global intervaloespec
        intervaloespec = self.intervaloEspectro.GetValue()



    def showFrame(self, msg):
        """
        Shows the frame and shows the message sent in the
        text control
        """
        self.regiaoAjusteI.SetValue(msg.data)

    def openFile(self, event):
        wildcard = "Dat (*.dat)|*.dat|" \
        "All files (*.*)|*.*"
        dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", wildcard, wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            global nomeAbre3
            nomeAbre3 = dialog.GetPath()
            self.editname.SetValue("%s" % (nomeAbre3))

        dialog.Destroy()




    def openFile2(self, event):
        wildcard = "CHN (*.chn)|*.chn|" \
        "Dat (*.dat)|*.dat|" \
        "All files (*.*)|*.*"
        dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", wildcard, wx.OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            self.nomeFundo = dialog.GetPath()
            self.editname2.SetValue("%s" % (self.nomeFundo))

        dialog.Destroy()

    def OnMove(self, event, valorInicial, escolha):
        if escolha == 1:
            self.regiaoAjusteI.SetValue("%s" % (str(valorInicial)))
            event.Skip()
        if escolha == 2:
            self.regiaoAjusteF.SetValue("%s" % (str(valorInicial)))
            event.Skip()
        if escolha == 3:
            self.posicaoPicosCaixa.SetValue("%s" % (' '.join(map(str, pico3))))
            event.Skip()
        if escolha == 4:
            self.inicioEspectro.SetValue("%s" % (str(iniespec)))
            event.Skip()
        if escolha == 5:
            self.intervaloEspectro.SetValue("%s" % (str(intervaloespec)))
            event.Skip()



class MatplotPanel(wx.Panel):

    def __init__(self, parent):
        global ini3
        self.mem = 0
        self.t=[]
        self.maxi=0

        self.ini = 0
        self.fin = 0
        self.pico = []

        wx.Panel.__init__(self, parent)
        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self, -1, self.figure)

        self.toolbar = NavigationToolbar(self.canvas)

        self.toolbar.Realize()      

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.abreEspectro = wx.Button(self, -1, "Iniciar",size=(100,30))
        self.sizer.Add(self.abreEspectro,wx.ALIGN_TOP)
        self.Bind(wx.EVT_BUTTON, self.modDraw, self.abreEspectro)       

        self.sizer.Add(self.toolbar, 0, wx.LEFT | wx.TOP | wx.GROW)
        self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)


        self.SetSizer(self.sizer)
        self.Fit()
        self.cid_up = self.figure.canvas.mpl_connect('button_press_event', self.OnClick)
        self.cid_press = self.figure.canvas.mpl_connect('key_press_event', self.press)
        self.msg = self.mem


    def draw(self):
        self.t = np.arange(0.0, 300.0, 0.01)
        self.axes.plot(self.t)

    def modDraw(self, event):
        self.abre = open(nomeAbre3)
        self.t=(self.abre.read()).split('\n')

        self.axes.clear()        

        self.axes.plot(self.t)

        self.figure.canvas.draw()

        '''self.axes.set_xlim([0,len(t)])'''

    def OnClick(self, event):
        if event.dblclick:
            print("DBLCLICK", event)
        else:
            print("DOWN    ", event.xdata)
            self.mem = event.xdata

    def press(self, event):
        print('press', event.key)
        sys.stdout.flush()
        if event.key=='i':
            global ini3
            ini3 = int(round(self.mem))
            self.ini = round(self.mem)

            print(self.ini)

        if event.key=='o':
            global fin3
            fin3 = int(round(self.mem))
            self.fin = round(self.mem)
            print(self.fin)

        if event.key=='p':
            global pico3
            pico3.append(int(round(self.mem)))
            self.pico.append(round(self.mem))
            print(self.pico)

        if event.key=='q':
            global pico3
            global fin3
            global ini3
            fin3 = 0
            ini3 = 0
            pico3 = []
            print(ini3)

        if event.key=='w':
            global pico3
            if len(pico3)>=1:
                del pico3[-1]
            print(fin3)

        if event.key=='e':
            global iniespec
            global intervaloespec
            self.axes.set_xlim([int(iniespec),int(iniespec)+int(intervaloespec)])
            maxim = self.maxY(int(iniespec),int(iniespec)+int(intervaloespec))
            self.axes.set_ylim(0,maxim+100)
            self.figure.canvas.draw()

        if event.key=='d':
            global iniespec
            global intervaloespec
            iniespec = int(iniespec)+int(intervaloespec) - 10
            self.axes.set_xlim([int(iniespec),int(iniespec)+int(intervaloespec)+10])
            maxim = self.maxY(int(iniespec),int(iniespec)+int(intervaloespec))
            self.axes.set_ylim(0,maxim+100)
            self.figure.canvas.draw()


    def maxY(self,a,b):
        self.maxi = int(self.t[a])
        for i in range(a,b):
            if self.maxi < int(self.t[i]):
                self.maxi = int(self.t[i])
        return(self.maxi)

app = wx.App(redirect=False)
frame = TestFrame(None, 'Hello World!')
frame.Show()
app.MainLoop()
#-*-编码:拉丁语-1-*-
导入wx
导入系统
进口numpy
将numpy作为np导入
导入matplotlib
导入操作系统
将matplotlib.pyplot作为plt导入
matplotlib.use('WXAgg')
从matplotlib.figure导入图形
从matplotlib.backends.backend_wxagg导入\
图CAVASWXAGG作为FigCanvas\
导航工具栏2WXAGG作为导航工具栏
从matplotlib.backends.backend_wxagg导入FigureCanvasWxAgg as FigureCanvas
从matplotlib.backends.backend_wx导入导航工具栏2wx
ini3=0
fin3=0
pico3=[]
nomeAbre3=''
iniespec=0
间隔规范=400
类TestFrame(wx.Frame):
定义初始(自我、父母、头衔):
wx.Frame.\uuuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuux(self,parent,title
self.sp=wx.SplitterWindow(self)
self.p1=主面板(self.sp)
self.p2=MatplotPanel(self.sp)
self.statusbar=self.CreateStatusBar()
self.box=wx.BoxSizer(wx.VERTICAL)
self.box.Add(self.p1,1,wx.EXPAND)
self.box.Add(self.p2,1,wx.EXPAND)
self.p2.draw()
self.create_菜单()
self.SetAutoLayout(真)
自设置器(自盒)
self.Layout()
self.statusbar.SetStatusText('Oi')
def创建菜单(自我):
self.menubar=wx.menubar()
menu_file=wx.menu()
m_expt=menu_file.Append(-1,“&Save plot\tCtrl-S”,“将绘图保存到文件”)
菜单\文件.AppendSeparator()
m_exit=menu_file.Append(-1,“E&xit\tCtrl-X”,“exit”)
menu_help=wx.menu()
m_about=菜单\u help.Append(-1,“&about\tF1”,“关于演示”)
self.menubar.Append(菜单文件“&file”)
self.menubar.Append(菜单\帮助“&help”)
self.SetMenuBar(self.menubar)
类主面板(wx.Panel):
定义初始化(自身,父级):
“”“创建主面板及其上的所有控件:
*mpl画布
*mpl导航工具栏
*交互控制面板
"""
self.nomeAbre='Spectrum'
self.nomeFundo='Spectrum2'
self.tipodoajust='1'
self.grauDoFundo='2'
self.posicaoDosPicos=''
self.regiaoDoAjusteI=''
self.regiaoDoAjusteF=“”
wx.Panel.\uuuuu init\uuuuuuuuuuuux(自,父)
self.dpi=100
self.fig=图((10.0,6.0),dpi=self.dpi)
self.abreArquivo=wx.按钮(self,-1,“打开:,(10,10))
self.Bind(wx.EVT_按钮、self.openFile、self.abreArquivo)
self.editname=wx.TextCtrl(self,value=self.nomeAbre,pos=(100,10),size=(200,-1))
self.AbreArquivoFundo=wx.按钮(self,-1,“Fundo:,(10,40))
self.editname2=wx.TextCtrl(self,value=self.nomeFundo,pos=(100,40),size=(200,-1))
self.Bind(wx.EVT_按钮、self.openFile2、self.AbreArquivoFundo)
self.ajusteTipo=wx.StaticText(self,-1,“A:,pos=(20,80))
self.ajusteTipoCaixa=wx.TextCtrl(self,value=self.tipodoajust,pos=(100,78),size=(25,-1))
self.fundoGrau=wx.StaticText(self,-1,“B参数:”,pos=(140,80))
self.fundoGrauCaixa=wx.TextCtrl(self,value=self.grauDoFundo,pos=(220,78),size=(25,-1))
self.posicaoPicos=wx.StaticText(self,-1,“Posicao-dos-Picos(最多10个值):”,pos=(20120))
self.posicaoPicosCaixa=wx.TextCtrl(self,value=self.posicaopospicos,pos=(245118),size=(350,-1))
self.regiaoAjuste=wx.StaticText(self,-1,“regiaoAjuste:”,pos=(20160))
self.regiaoAjusteI=wx.TextCtrl(self,value='',pos=(140158),size=(60,-1))
self.regiaoAjusteF=wx.TextCtrl(self,value='',pos=(220158),size=(60,-1))
self.iniciospectrotexto=wx.StaticText(self,-1,“Start:,pos=(250302))
self.iniciospectro=wx.TextCtrl(self,-1,“,pos=(300300))
self.Bind(wx.EVT_TEXT、self.functiniciospectro、self.iniciospectro)
self.intervaloeSpectrocTexto=wx.StaticText(self,-1,“Interval:,pos=(420,302))
self.intervaloEspectro=wx.TextCtrl(self,-1,“400”,pos=(490300),size=(50,-1))
self.Bind(wx.EVT_TEXT,self.funcintervaloEspectro,self.intervaloEspectro)
self.idf=wx.按钮(self,-1,“idf”,(100300))
self.Bind(wx.EVT_按钮、self.craidf、self.idf)
全球ini3
全球金融机构3
全球pico3
全球范围内
全球间隔规范
self.Bind(wx.EVT_运动,lambda事件:self.OnMove(事件,ini3,1))
self.Bind(wx.EVT_运动,lambda事件:self.OnMove(事件,fin3,2))
self.Bind(wx.EVT_运动,lambda事件:self.OnMove(事件,pico3,3))
self.Bind(wx.EVT_运动,lambda事件:self.OnMove(事件,inspect,4))
self.Bind(wx.EVT_运动,lambda事件:self.OnMove(事件,intervaloespec,5))
def criaIDF(自身、事件):
a1=nomeAbre3
a2=str(len(pico3))+','+str(ini3)+','+str(fin3)+','
对于pico3中的j:
a2+=str(j)+','
a2+=','
印刷品(a1)
印刷品(a2)
def FUNCINICIOSPECTRO(自身、事件):
全球范围内
INIESPECT=self.INICIOSPECTRO.GetValue()
def funcintervaloEspectro(自身、事件):
全球间隔规范
intervaloespec=self.intervaloEspectro.GetValue()
def显示框(自身,消息):
"""
显示帧并显示在中发送的消息
文本控件
"""
self.regiaoAjusteI.SetValue(msg.data)
def openFile(自身、事件):
通配符=“Dat(*.Dat)|*.Dat |”\
“所有文件(*.*)|*.*”
dialog=wx.FileDialog(无,“选择文件”,os.getcwd(),”,通配符,wx.OPEN)
如果dialog.showmodel()==wx.ID\u正常:
全球nomeAbre3
笔名
        self.box.Add(self.p1, 1, wx.EXPAND)
        self.box.Add(self.p2, 1, wx.EXPAND)
        self.box.Add(self.sp, 1, wx.EXPAND)
        self.sp.SplitVertically(self.p1, self.p2)
        self.SetMinSize((350, 350))