gpt4 book ai didi

python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-04 04:08:35 25 4
gpt4 key购买 nike

我几乎完成了图形界面的试用版。我在 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()

最佳答案

您遇到的主要问题是,当您应该添加拆分器窗口本身时,却将拆分器窗口的子级添加到BoxSizer self.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)

最后,由于您没有为 MainPanel 使用 Sizer,因此您可能需要为其设置最小尺寸。我发现,如果我没有指定最小尺寸(或者没有使用 SetMinimumPanelSize 限制分割器窗口中 Pane 的最小尺寸),则分割器窗口根本不会显示 MainPanel。尝试将以下内容添加到 MainPanel.__init__ 的底部(如果您愿意,可以更改数字):

        self.SetMinSize((350, 350))

我正在 openSUSE 12.3 x64 上的 Python 2.7.3 上使用 wxWidgets/wxPython 2.9.4,如果有帮助的话。

关于python - 从 Windows 迁移到 Linux 时 WXPython 接口(interface)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20180613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com