1

我想将选定的文本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()
4

2 回答 2

2

您可以通过以下方式在事件处理程序中设置样式:

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

或将其改回:

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

使用 wxPython 2.8.12 在 Ubuntu 12.04 上成功测试。

于 2013-03-27T10:20:43.150 回答
0

默认为wxTextCtrl左对齐。wxTE_RIGHT您可以使用windows 样式向右对齐,但它仅适用于 Windows 和 GTK2。

于 2013-03-27T06:00:25.853 回答