Python/wxWidgets: Vertically aligning text in a wrapped StaticText -


i've seen lot of posts around internet none of advice works.

here test code:

import wx class displaytext(wx.dialog):      def __init__(self, parent, text="", displaymode=0):          # initialize dialog         wx.dialog.__init__(self, parent, size=(480,320), style=( wx.dialog_ex_metal | wx.stay_on_top ) )          # freeze ui user won't see stuff flashing         self.freeze()          # (for mac) setup panel         self.panel = wx.panel(self,size=(480,320))         self.panel.setbackgroundcolour(wx.colour(0,0,128))          # setup sizer vertical centering         self.sizer = wx.boxsizer(wx.horizontal)          # create text field         self.txtfield = wx.statictext(self.panel,size=(448,-1),style=wx.align_centre_horizontal)         self.txtfield.setlabel(text)         self.txtfield.wrap(448)         self.txtfield.setlabel(self.txtfield.getlabel())         self.txtfield.setfont(wx.font(36, wx.default, wx.bold, 0))               self.txtfield.setforegroundcolour(wx.colour(255,255,255))          # add static text sizer         self.sizer.add(self.txtfield,1, 0,15)         self.panel.setsizer(self.sizer)          # ensure layouts applied         self.sizer.layout()          # thaw ui         self.thaw()          # center form         self.center()  app = wx.app(false)  c = displaytext(none, text="now time men come aid of country.") c.show() import wx.lib.inspection  wx.lib.inspection.inspectiontool().show()  app.mainloop() 

based on understanding of statictext, problem seems when call wrap() function, control wraps text not change control's size. means sizer, unaware of actual vertical size of wrapped text, cannot reposition static text.

one post found suggested using wx.expand, not solve problem. allowing sizer expand statictext makes statictext fill entire frame, , since statictext doesn't vertically center in , of itself, text end @ top of form, not center.

another post suggested experimenting wx.align_center_vertical in sizer, didn't either, because same problem remains - sizer can work on size of control, , control's size not changing after text wrapped , re-rendered.

various attempts have resulted in either text wrapped @ top of form, first line being shown in center of form, first line being shown @ top of form, , text not being wrapped , flowing off right hand edge of form. no combination of i've read has worked vertically center wrapped text on form.

what therefore have able somehow figure out size of text vertically after wrapping has taken place. can't assumed because on different platforms, or on same platform, or based on text itself, font's vertical pixel size may differ. needs somehow calculated.

i'm thinking if can somehow figure out actual size of rendered text inside statictext control, explicitly set control's size , let sizer work.

this seems should relatively simple thing accomplish...

advice appreciated!

the key static text style; needed enable multi-line style wrap work: wx.te_multiline.

i set layout on panel itself, not sizer might not related problem.

on machine, windows 7 64bit, needed change setfont call; might need change back.

import math threading import thread import time import wx  e = lambda x: complex(math.cos(x), 0) + complex(0, math.sin(x)) r = lambda x: ((e(0*math.pi/3.0)*e(x)).real + 1)/2.0 g = lambda x: ((e(2*math.pi/3.0)*e(x)).real + 1)/2.0 b = lambda x: ((e(4*math.pi/3.0)*e(x)).real + 1)/2.0 colo = lambda rad: map(lambda x: int(128 * x(rad)), [r, g, b])  class displaytext(wx.dialog):      def __init__(self, parent, text="", displaymode=0):          # initialize dialog         wx.dialog.__init__(self, parent, size=(480, 320), style=( wx.dialog_ex_metal | wx.stay_on_top ) )          # freeze ui user won't see stuff flashing         self.freeze()          # (for mac) setup panel         self.panel = wx.panel(self, size=(480, 320))         self.panel.setbackgroundcolour(wx.colour(0, 0, 128))         self.panel.setbackgroundcolour(wx.colour(*colo(0)))          # setup sizer vertical centering         self.sizer = wx.boxsizer(wx.horizontal)         self.panel.setsizer(self.sizer)          # create text field         self.txtfield = wx.statictext(self.panel, size=(448, -1), style=(wx.align_centre_horizontal | wx.te_multiline ))         self.txtfield.setfont(wx.font(36, wx.fontfamily_default, wx.fontstyle_normal, wx.fontweight_bold))               self.txtfield.setforegroundcolour(wx.colour(255, 255, 255))         self.txtfield.setlabel(text)         self.txtfield.wrap(448)         self.sizer.add(self.txtfield, 1, 0, 15)          # add static text sizer         # ensure layouts applied         self.panel.layout()         self.panel.refresh()          # thaw ui         self.thaw()          # center form         self.center()          self.angle = 0         self.angle_inc = (2*math.pi)/25.0      def change_colour(self):         t = thread(target=self.epilepsy_mode)         t.start()       def epilepsy_mode(self):         while true:             time.sleep(0.02)             self.panel.setbackgroundcolour(wx.colour(*colo(self.angle)))             self.panel.refresh()             self.angle = (self.angle + self.angle_inc) % (2*math.pi)  app = wx.app(false)  c = displaytext(none, text="now time men come aid of country.") c.show() #import wx.lib.inspection  #wx.lib.inspection.inspectiontool().show()  c.change_colour()  app.mainloop() 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -