2012-01-03 9 views
0

私は、サイズが大きすぎると楕円で切り捨てられ、適合するテキストを表示する静的テキストを作成しようとしています。自己切り捨て楕円形のテキストを作成するctrl:ペイントイベントを強制する方法

ペイントイベントを送信する必要があるウィンドウのサイズを変更するとうまく動作しているようですが、ラベルテキストを設定するとペイントイベントが発生しないようです。

ペイントイベントをフレームに送信するだけでなく、フレーム(パネル、パネル、テキスト自体)のすべてのことをリフレッシュと更新を呼び出してみましたが、正しく動作するようにはできません。

私は、テキストラベルを変更するためのボタン付きのデモを作成しました。

EDIT:トリックは..私はその方法を忘れておくなかったテキストの親にLayout()を呼び出して、今うまく働く上でコードを編集

import wx 
from wx.lib.stattext import GenStaticText as StaticText 

class EllipticStaticText2(StaticText): 
    def __init__(self,parent,id=wx.ID_ANY,label='',width=None,pos=wx.DefaultPosition,size=wx.DefaultSize,style=0,name="ellipticstatictext2"): 
     self.actual_label= label 
     self.parent= parent 
     self.width= width 
     StaticText.__init__(self,parent,id,label,pos,size,style,name) 
     self.Bind(wx.EVT_PAINT, self.OnPaint2) 

    def SetLabel(self,label): 
     self.actual_label= label 
     StaticText.SetLabel(self,label) 
     self.parent.Layout() 

    def SetLabelText(self,label): 
     self.actual_label= label 
     StaticText.SetLabelText(self,label) 
     self.parent.Layout() 

    def OnPaint2(self,event): 
     dc= wx.PaintDC(self) 
     displaylabel= self.actual_label 
     x,y= dc.GetTextExtent(displaylabel) 
     self.w,self.h= self.GetSize() 
     if x>self.w: 
      while x>self.w: 
       displaylabel= displaylabel[:-1] 
       x,y= dc.GetTextExtent(displaylabel+"...") 
      StaticText.SetLabel(self,displaylabel+"...") 
     else: 
      StaticText.SetLabel(self,displaylabel) 
     event.Skip() 


class CustomFrame(wx.Frame): 
    def __init__(self,parent,id,name,size): 
     wx.Frame.__init__(self,parent,id,name,size) 

     self.panel = wx.Panel(self, -1) 
     sizer = wx.BoxSizer(wx.VERTICAL) 
     self.index= 35 

     self.elliptic = EllipticStaticText2(self.panel, -1, r"long string."*20) 
     whitePanel = wx.Panel(self.panel, -1) 
     whitePanel.SetBackgroundColour(wx.WHITE) 

     sizer.Add(self.elliptic, 0, wx.ALL|wx.EXPAND, 10) 
     btn= wx.Button(self.panel,-1,"change") 
     sizer.Add(btn,0,wx.ALL|wx.EXPAND, 10) 
     sizer.Add(whitePanel, 1, wx.ALL|wx.EXPAND, 10) 

     btn.Bind(wx.EVT_BUTTON,self.button) 

     self.panel.SetSizer(sizer) 
     sizer.Layout() 

    def button(self,event): 
     self.elliptic.SetLabel(chr(self.index)*100) 
     self.index+=1 
     self.Refresh() 
     self.panel.Refresh() 
     self.elliptic.Refresh() 

if __name__ == "__main__": 
    app = wx.PySimpleApp() 

    frame = CustomFrame(None, -1, "EllipticStaticText", size=(700, 300)) 

    frame.CenterOnScreen() 
    frame.Show() 

    app.MainLoop() 

答えて

0

を解決しよう!

関連する問題