2013-03-27 4 views
0

wxTextCtrl에 선택한 텍스트를 왼쪽 또는 오른쪽으로 정렬하고 싶습니다. 이것은 전체 프로그램의 코드입니다. 텍스트를 오른쪽으로 정렬하고 버튼 클릭만으로 왼쪽으로 정렬해야합니다.wxTextCtrl에서 동적으로 텍스트를 정렬하는 방법은 무엇입니까?

class Beditor(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title, size=(800,600)) 

     self.SetIcon(wx.Icon('/usr/share/ezhuthani/icons/icon.png', wx.BITMAP_TYPE_PNG)) 

     ## variables 
     self.modify = False 
     self.last_name_saved = '' 
     self.replace = False 
     self.word = '' 


    ## Class Methods 
    def NewApplication(self, event): 
     editor = Beditor(None, -1, window_title + '[Untitled/പേരു നൽകിയിട്ടില്ല]' ) 
     editor.Centre() 
     editor.Show() 

    def OnOpenFile(self, event): 
     file_name = os.path.basename(self.last_name_saved) 
     if self.modify: 
      dlg = wx.MessageDialog(self, 'Save changes?/മാറ്റങ്ങൾ സംഭരിക്കേണ്ടതുണ്ടോ?', '', wx.YES_NO | wx.YES_DEFAULT | wx.CANCEL | 
         wx.ICON_QUESTION) 
      val = dlg.ShowModal() 
      if val == wx.ID_YES: 
       self.OnSaveFile(event) 
       self.DoOpenFile() 
      elif val == wx.ID_CANCEL: 
       dlg.Destroy() 
      else: 
       self.DoOpenFile() 
     else: 
      self.DoOpenFile() 

    def DoOpenFile(self): 
     wcd = 'All files (*)|*|Editor files (*.ef)|*.ef|' 
     dir = os.getcwd() 
     open_dlg = wx.FileDialog(self, message='Choose a file/ഒരു രേഖ തിരഞ്ഞെടുക്കുക', defaultDir=dir, defaultFile='', 
         wildcard=wcd, style=wx.OPEN|wx.CHANGE_DIR) 
     if open_dlg.ShowModal() == wx.ID_OK: 
      path = open_dlg.GetPath() 

      try: 
       file = open(path, 'r') 
       text = file.read() 
       file.close() 
       if self.text.GetLastPosition(): 
        self.text.Clear() 
       self.text.WriteText(text) 
       self.last_name_saved = path 
       self.statusbar.SetStatusText('', 1) 
       self.modify = False 
       self.SetTitle(window_title + path) 

      except IOError, error: 
       dlg = wx.MessageDialog(self, 'Error opening file/രേഖ തുറക്കാന്‍ കഴിയില്ല\n' + str(error)) 
       dlg.ShowModal() 

      except UnicodeDecodeError, error: 
       dlg = wx.MessageDialog(self, 'Error opening file/രേഖ തുറക്കാന്‍ കഴിയില്ല\n' + str(error)) 
       dlg.ShowModal() 

     open_dlg.Destroy() 
    def OnSaveFile(self, event): 
     if self.last_name_saved: 

      try: 
       file = open(self.last_name_saved, 'w') 
       text = self.text.GetValue() 
       file.write(text.encode('utf-8')) 
       file.close() 
       self.statusbar.SetStatusText(os.path.basename(self.last_name_saved) + ' saved/സംഭരിച്ചു', 0) 
       self.modify = False 
       self.statusbar.SetStatusText('', 1) 

      except IOError, error: 
       dlg = wx.MessageDialog(self, 'Error saving file/രേഖ സംഭരിക്കാൻ കഴിയില്ല\n' + str(error)) 
       dlg.ShowModal() 
     else: 
      self.OnSaveAsFile(event) 

    def OnSaveAsFile(self, event): 
     wcd='All files(*)|*|Editor files (*.ef)|*.ef|' 
     dir = os.getcwd() 
     save_dlg = wx.FileDialog(self, message='Save file as.../വേറേതായി സംഭരിക്കുക...', defaultDir=dir, defaultFile='', 
         wildcard=wcd, style=wx.SAVE | wx.OVERWRITE_PROMPT) 
     if save_dlg.ShowModal() == wx.ID_OK: 
      path = save_dlg.GetPath() 

      try: 
       file = open(path, 'w') 
       text = self.text.GetValue() 
       file.write(text.encode('utf-8')) 
       file.close() 
       self.last_name_saved = os.path.basename(path) 
       self.statusbar.SetStatusText(self.last_name_saved + ' saved/സംഭരിച്ചു', 0) 
       self.modify = False 
       self.statusbar.SetStatusText('', 1) 
       self.SetTitle(window_title + path) 
      except IOError, error: 
       dlg = wx.MessageDialog(self, 'Error saving file/രേഖ സംഭരിക്കാൻ കഴിയില്ല\n' + str(error)) 
       dlg.ShowModal() 
     save_dlg.Destroy() 

    def OnCut(self, event): 
     self.text.Cut() 

    def OnCopy(self, event): 
     self.text.Copy() 

    def OnPaste(self, event): 
     self.text.Paste() 

    def QuitApplication(self, event): 
     if self.modify: 
      dlg = wx.MessageDialog(self, 'Save before Exit?/പുറത്തു പോകുന്നതിനു മുൻപ് രേഖ സംഭരിക്കേണ്ടതുണ്ടോ?', '', wx.YES_NO | wx.YES_DEFAULT | 
         wx.CANCEL | wx.ICON_QUESTION) 
      val = dlg.ShowModal() 
      if val == wx.ID_YES: 
       self.OnSaveFile(event) 
       if not self.modify: 
        wx.Exit() 
      elif val == wx.ID_CANCEL: 
       dlg.Destroy() 
      else: 
       self.Destroy() 
     else: 
      self.Destroy() 

    def OnDelete(self, event): 
     frm, to = self.text.GetSelection() 
     self.text.Remove(frm, to) 

    def OnSelectAll(self, event): 
     self.text.SelectAll() 

    def OnTextChanged(self, event): 
     self.modify = True 
     self.statusbar.SetStatusText(' modified', 1) 
     event.Skip() 

    def OnKeyDown(self, event): 
     keycode = event.GetKeyCode() 
     if keycode == wx.WXK_INSERT: 
      if not self.replace: 
       self.statusbar.SetStatusText('INS', 2) 
       self.replace = True 
      else: 
       self.statusbar.SetStatusText('', 2) 
       self.replace = False 
     event.Skip() 

    def ToggleStatusBar(self, event): 
     if self.statusbar.IsShown(): 
      self.statusbar.Hide() 
     else: 
      self.statusbar.Show() 

    def StatusBar(self): 
     self.statusbar = self.CreateStatusBar() 
     self.statusbar.SetStatusStyles([wx.SB_VERTICAL]) 
     self.statusbar.SetFieldsCount(3) 
     self.statusbar.SetStatusWidths([-13, -3, -2]) 

    def LayoutHelp(self, event): 
     info = wx.AboutDialogInfo() 

     info.SetIcon(wx.Icon('/usr/share/ezhuthani/icons/ezhuthani.png', wx.BITMAP_TYPE_PNG)) 
     info.SetName('Layout') 
     wx.AboutBox(info) 

    def ToggleConvInToolbar(self, event): 
     if self.convert.IsChecked(): 
      toggle = True 
     else: 
      toggle = False 

     self.toolbar.ToggleTool(504, toggle) 

    def ToggleConvInMenu(self, event): 
     self.convert.Toggle() 


    def OnAbout(self, event): 

     description = """എഴുത്താണി """ """\n A Malayalam Phonetic Text Editor for GNU/Linux systems\n""" 

     licence = """  
     എഴുത്താണി is a FREE software; you can redistribute it and/or modify it under the 
       terms of the GNU General Public License as published by the Free Software Foundation; 
       either version 3 of the License, or (at your option) any later version. 

     എഴുത്താണി is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
     without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE. See the GNU General Public License for more details. You should have received 
     a copy of the GNU General Public License along with എഴുത്താണി ; if not, write to the Free 
     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 0211""" 

     info = wx.AboutDialogInfo() 

     info.SetIcon(wx.Icon('/usr/share/ezhuthani/icons/logo.png', wx.BITMAP_TYPE_PNG)) 
     info.SetName('എഴുത്താണി') 
     info.SetVersion('0.1') 
     info.SetDescription(description) 
     info.SetCopyright('Developed by IIITM-K Trivandrum') 
     info.SetWebSite('http://www.iiitmk.ac.in/vrclc/') 
     info.SetLicence(licence) 
     info.AddDeveloper('Christy , Arun') 
     info.AddDocWriter('Arun, Christy') 
     info.AddArtist('The IIITMK crew :-)') 

     info.SetName('എഴുത്താണി') 
     wx.AboutBox(info) 



    def FontSel(self, event): 
     default_font = wx.Font(14, wx.DEFAULT, wx.NORMAL, wx.NORMAL, 0, "") 
     data = wx.FontData() 

     data.SetAllowSymbols(False) 
     data.SetInitialFont(default_font) 
     data.SetRange(10, 30) 
     dlg = wx.FontDialog(self, data) 
     if dlg.ShowModal() == wx.ID_OK: 
      data = dlg.GetFontData() 
      font = data.GetChosenFont() 
      color = data.GetColour() 
      text = 'Face: %s, Size: %d, Color: %s' % (font.GetFaceName(), font.GetPointSize(), color.Get()) 
      self.text.SetFont(font) 
     dlg.Destroy() 

    def PreviewConv(self, event): 
     keycode = event.GetKeyCode() 


     if not self.convert.IsChecked(): 
      if 32 < keycode <= 126: 
       key = chr(keycode) 
       self.word += key 
       self.statusbar.SetStatusText(engine.roman2beng(self.word.encode('utf-8')),0) 
      elif keycode == wx.WXK_SPACE: 
       self.statusbar.SetStatusText('',0) 
       self.word = '' 
      elif keycode == wx.WXK_HOME or keycode == wx.WXK_END: 
       self.statusbar.SetStatusText('',0) 
       self.word = '' 

      else: 
       event.Skip() 
       text = self.text.GetRange(0, self.text.GetInsertionPoint()-1) 

       sow = text.rfind(' ') ## sow = start of word (caret position) 

       if sow == -1:   ## you are at the start of document, so remove the initial space 
        sow = 0  

       self.word = self.text.GetRange(sow, self.text.GetInsertionPoint()-1) 
       self.statusbar.SetStatusText(engine.roman2beng(self.word.encode('utf-8')),0) 


     else: 
      self.statusbar.SetStatusText('',0) 
      self.word = '' 

     event.Skip() 

    def populateStore(self, event): 
     keycode = event.GetKeyCode() 
     if keycode == wx.WXK_SPACE: 
      event.Skip() 
      stockUndo.append(self.text.GetValue()) 
      self.toolbar.EnableTool(808, True) 
      if len(stockUndo) > depth: 
       del stockUndo[0] 

     event.Skip() 


     ###### converting to Malayalam using engine ########## 
    def conv(self, event): 
     ## New Algorithm for Ver 1.2 
     keycode = event.GetKeyCode() 

     if keycode == wx.WXK_SPACE: 
     #if keycode == event.GetKeyCode(): 
      text = self.text.GetRange(0, self.text.GetInsertionPoint()) 
      wordlist = text.split(' ') 
      cur_word = ' ' + wordlist[-1]  ## cur_word = current word 
      sow = text.rfind(' ') ## sow = start of word (caret position) 

      #event.Skip()   

      if sow == -1:   ## you are at the start of document, so remove the initial space 
       sow = 0 
       cur_word = cur_word[1:] 

      if not self.convert.IsChecked(): 
       self.text.Replace(sow, self.text.GetInsertionPoint(), engine.roman2beng(cur_word.encode('utf-8'))) 


     event.Skip() 



app = wx.App() 
Beditor(None, -1, window_title + '[Untitled]/പേരു നൽകിയിട്ടില്ല') 
app.MainLoop() 

답변

0

당신은 이벤트 처리기에서 이런 식으로 스타일을 설정할 수 있습니다 wxPython을 2.8.12과 우분투 12.04에서 성공적으로 테스트

self.textCtrl.SetWindowStyle(style & ~wx.TE_RIGHT | wx.TE_LEFT) 

합니다.

0

wxTextCtrl의 기본값은 왼쪽 정렬입니다. wxTE_RIGHT 창 스타일을 사용하여 오른쪽 정렬 할 수 있지만 Windows 및 GTK2에서만 작동합니다.

style = self.textCtrl.GetWindowStyle() 
self.textCtrl.SetWindowStyle(style & ~wx.TE_LEFT | wx.TE_RIGHT) 

를하거나 다시 변경 :

+0

이것은 텍스트 편집기입니다. 버튼 클릭시 스타일을 통합해야합니다. –

+0

나는 당신을 코드에서 모았다. 먼저 윈도우 스타일을 하드 코딩하여 포함 시켜서 작동하는지 확인한 다음 위젯을 통해 설정/해제를 시도하는 것이 좋습니다. – Anthon

+0

UBUNTU 10.04를 사용하고 있습니다. –

관련 문제