Tengo un problema con wxPython. A continuación, se publica una versión simplificada del código (se eliminan espacios en blanco, comentarios, etc. para reducir el tamaño, pero el formato general de mi programa se mantiene aproximadamente igual). Cuando ejecuto el script, el texto estático se ajusta correctamente como debería, pero los otros elementos del panel no se mueven hacia abajo (actúan como si el texto estático fuera solo una línea y, por lo tanto, no todo está visible). Si cambio manualmente el tamaño de la ventana/marco, incluso solo una pequeña cantidad, todo se corrige y se muestra como debería. Tomé capturas de pantalla para mostrar este comportamiento, pero acabo de crear esta cuenta y, por lo tanto, no tengo los 10 puntos de reputación necesarios para poder publicar imágenes.wxProblemas de Piython con Wrapping StaticText
¿Por qué no se muestra correctamente para empezar? Intenté todo tipo de combinaciones de GetParent(). Refresh() o Update() y GetTopLevelParent(). Update() o Refresh(). He intentado todo lo que puedo pensar pero no puedo mostrar correctamente sin cambiar manualmente el tamaño del marco/ventana. Una vez redimensionado, funciona exactamente como yo quiero.
información:
Windows XP
Python 2.5.2
wxPython 2.8.11.0 (RSU-Unicode)
¿Alguna sugerencia? ¡Gracias!
Código:
#! /usr/bin/python
import wx
class StaticWrapText(wx.PyControl):
def __init__(self, parent, id=wx.ID_ANY, label='', pos=wx.DefaultPosition,
size=wx.DefaultSize, style=wx.NO_BORDER,
validator=wx.DefaultValidator, name='StaticWrapText'):
wx.PyControl.__init__(self, parent, id, pos, size, style, validator, name)
self.statictext = wx.StaticText(self, wx.ID_ANY, label, style=style)
self.wraplabel = label
#self.wrap()
def wrap(self):
self.Freeze()
self.statictext.SetLabel(self.wraplabel)
self.statictext.Wrap(self.GetSize().width)
self.Thaw()
def DoGetBestSize(self):
self.wrap()
#print self.statictext.GetSize()
self.SetSize(self.statictext.GetSize())
return self.GetSize()
class TestPanel(wx.Panel):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Panel.__init__(self, *args, **kwargs)
self.createControls()
def createControls(self):
# --- Panel2 -------------------------------------------------------------
self.Panel2 = wx.Panel(self, -1)
msg1 = 'Below is a List of Files to be Processed'
staticBox = wx.StaticBox(self.Panel2, label=msg1)
Panel2_box1_v1 = wx.StaticBoxSizer(staticBox, wx.VERTICAL)
Panel2_box2_h1 = wx.BoxSizer(wx.HORIZONTAL)
Panel2_box3_v1 = wx.BoxSizer(wx.VERTICAL)
self.wxL_Inputs = wx.ListBox(self.Panel2, wx.ID_ANY, style=wx.LB_EXTENDED)
sz = dict(size=(120,-1))
wxB_AddFile = wx.Button(self.Panel2, label='Add File', **sz)
wxB_DeleteFile = wx.Button(self.Panel2, label='Delete Selected', **sz)
wxB_ClearFiles = wx.Button(self.Panel2, label='Clear All', **sz)
Panel2_box3_v1.Add(wxB_AddFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_DeleteFile, 0, wx.TOP, 0)
Panel2_box3_v1.Add(wxB_ClearFiles, 0, wx.TOP, 0)
Panel2_box2_h1.Add(self.wxL_Inputs, 1, wx.ALL|wx.EXPAND, 2)
Panel2_box2_h1.Add(Panel2_box3_v1, 0, wx.ALL|wx.EXPAND, 2)
msg = 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
msg += 'This is a long line of text used to test the autowrapping '
msg += 'static text message. '
staticMsg = StaticWrapText(self.Panel2, label=msg)
Panel2_box1_v1.Add(staticMsg, 0, wx.ALL|wx.EXPAND, 2)
Panel2_box1_v1.Add(Panel2_box2_h1, 1, wx.ALL|wx.EXPAND, 0)
self.Panel2.SetSizer(Panel2_box1_v1)
# --- Combine Everything -------------------------------------------------
final_vbox = wx.BoxSizer(wx.VERTICAL)
final_vbox.Add(self.Panel2, 1, wx.ALL|wx.EXPAND, 2)
self.SetSizerAndFit(final_vbox)
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
# Init the base class
wx.Frame.__init__(self, *args, **kwargs)
panel = TestPanel(self)
self.SetClientSize(wx.Size(500,500))
self.Center()
class wxFileCleanupApp(wx.App):
def __init__(self, *args, **kwargs):
# Init the base class
wx.App.__init__(self, *args, **kwargs)
def OnInit(self):
# Create the frame, center it, and show it
frame = TestFrame(None, title='Test Frame')
frame.Show()
return True
if __name__ == '__main__':
app = wxFileCleanupApp()
app.MainLoop()
EDIT:
Ver mi post a continuación para una solución que funciona!
Debería haberlo pensado. Por lo general, desea llamar a Diseño en el elemento primario de los widgets o en el tamaño que contiene los widgets. Oh bien. Lamento no haberlo visto. –
¡Gracias por la ayuda! Todavía no funciona como debería cuando maximizo/minimizo la ventana. Pero por el momento, es lo suficientemente bueno. –
Ah, esto hace que funcione incluso cuando se usa el botón maximizar ... en la función de ajuste, use self.statictext.Wrap (self.GetParent(). GetSize(). Width) en lugar de self.statictext.Wrap (self .GetSize(). Width) –