2013-11-24 28 views
0

我幾乎完成了我的圖形界面的試用版。我在Windows上做了這個,然後我轉移到Linux來測試它。 我遇到的問題是Matplotlib面板正在被切割(僅在Linux版本上),所以我看不到圖形。我不能調整大小來解決這個問題。從Windows遷移到Linux時的WXPython界面無法正常工作

下面你可以看到我的代碼。有任何想法嗎?

# -*- 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() 
+0

你可以減少到最少量的代碼,將重現您的問題?有了這麼多的代碼,沒有多少人會費心閱讀你的問題。 – tacaswell

+0

謝謝,@tcaswell,但Luke WoodWard解決了我的問題。下次我這樣做。 :) – Zhozer

回答

0

您遇到的主要問題是,您要添加的孩子拆分窗口的BoxSizerself.box,當你要添加拆分窗口本身。

而是寫作的

 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) 

最後,因爲您沒有使用Sizer作爲您的MainPanel,所以您可能需要設置它的最小尺寸。我發現如果我沒有給它一個最小尺寸(或者沒有使用SetMinimumPanelSize來限制分隔窗口中窗格的最小尺寸),那麼分隔窗口根本沒有顯示MainPanel。嘗試加入以下的MainPanel.__init__底部(更改數字,如果你願意的話):

 self.SetMinSize((350, 350)) 

我在openSUSE 12.3的x64使用wxWidgets/wxPython的2.9.4關於Python 2.7.3,有沒有什麼幫助。

+0

謝謝,@Luke伍德沃德。這解決了我的問題。 :) – Zhozer