Can somebody tell me how to do bolding and Italicizing plus underlining
I have just started getting serious after long time "milk drinking", now I want to eat solid food.
Thanks all,
Steve

import wx
ID_EXIT =100
ID_BOLD = 101
ID_ITALIC = 102
ID_UNDERLINE = 103
ID_TEXT = 104
class SimpleEditor(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        #Add Menu Bar
        menubar = wx.MenuBar()
        self.SetMenuBar(menubar)
        #file menu is added
        filemenu = wx.Menu()
        menubar.Append(filemenu, '&File')
        filemenu.Append(ID_EXIT, '&Quit', 'Quit the Program')
        
        # define events
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)
        
        #Add panel
        MainPanel = wx.Panel(self, -1)
        
        # define sizers
        MainSizer = wx.BoxSizer(wx.VERTICAL)
        
        #toolbars font
        tb1 = wx.ToolBar(MainPanel, -1)
        tb1.AddLabelTool(wx.ID_BOLD, '', wx.Bitmap('./icon/format-text-bold.png'))
        tb1.AddLabelTool(wx.ID_ITALIC, '', wx.Bitmap('./icon/format-text-italic.png'))
        tb1.AddLabelTool(wx.ID_UNDERLINE, '', wx.Bitmap('./icon/format-text-underline.png'))
        #tb1.SetToolBitmapSize((24,24 ))
        tb1.Realize()
        #add to sizer
        MainSizer.Add(tb1, proportion = 0, flag = wx.EXPAND)
        
        #set up event handlers
        wx.EVT_MENU(self, ID_BOLD, self.OnBold)
        #wx.EVT_MENU(self, ID_ITALIC, self.OnItalic)
        #wx.EVT_MENU(self, ID_UNDERLINE, self.OnUnderline)
        
        # toolbar other ops
        tb2 = wx.ToolBar(MainPanel, -1)
        tb2.AddLabelTool(ID_EXIT, '', wx.Bitmap('./icon/process-stop.png'))
        tb2.Realize()
        MainSizer.Add(tb2, proportion = 0, flag = wx.EXPAND | wx.TOP, border = 0 )
        
        #Add text control
        Tarea = wx.TextCtrl(MainPanel, ID_TEXT, style = wx.TE_MULTILINE)
        #add to main sizer
        MainSizer.Add(Tarea, 1, wx.EXPAND |wx.LEFT |wx.RIGHT |wx.BOTTOM, 2)
        
        
        
        
        
        
        
        #set main sizer
        MainPanel.SetSizer(MainSizer)
        
        self.Center()
        self.Show(True)
        
        #start events exit
    def OnExit(self, event):
        mess = wx.MessageDialog(self, 'Are you sure you want to Quit?', 'Confirm', style = wx.YES |wx.NO |wx.ICON_QUESTION)
        ret = mess.ShowModal()
        if ret == wx.ID_YES:
            self.Destroy()
        else :
            SimpleEditor.event()    
            
        #start events edit toolbar
    def OnBold(self, event):
        selected = GetStringSelection(self)
        selected = selected.SetWeight(self, Bold)
        return selected
        

app = wx.App(False)
SimpleEditor(None, -1,'Text Editor')
app.MainLoop()

Recommended Answers

All 12 Replies

If you look at the top right-hand side of the page you will find this white box contained in a larger green box. It's called a search box and it's very useful...

Also, vegaseat gave us all a very nice example of using fancy text and the like in the sticky post about wxPython... Take the time to do a little digging and you'll find your answer.

Thanks for the reaminder,
But this forum is big and I tried to do search but hit a wall. If you know a snippet why don't you post a link?

I don't mean to argue but If you have something to help my problem pse do it and I'll appreciate.

Thanks again!!
Steve

Is there anybody with an Idea on How to do this??

Go to the wxPython page and download the Docs and Demos package. Run it and look at the TextCtrl example. That or look at the source code for CodeEditor, which should be packaged along with the demos.

Thanks, I'll check and feedback

Thanks

Anyway wx.Demo is very interesting but very complex; not for begginers! Nothing helped my friend.
I have tried to modify my Question so that it can now be much understandable:
I need three widgets, One TextCtrl and another then below it is button and textCtrl multiline.
Somebody types text on above textCtrl (tex1) and then at hitting return key the text goes to textctrl multiline (tex2). At hitting of a button (bold), the selected text turns bold.
Anyone to help on that welcomed :D
Steve

import wx
ID_BOLD = 10
class MainWindow(wx.Frame):
    def __init__ (self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size= (300, 250))
        
        #add panel
        panel = wx.Panel(self, -1)
        #define sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        #Add text control
        tex1 = wx.TextCtrl(panel, -1)
        tex2 = wx.TextCtrl(panel, -1, style = wx.TE_MULTILINE)
        bold = wx.Button(panel, ID_BOLD, "Bold")
        
        #Add  to sizers
        sizer.Add(tex1, 0 , wx.EXPAND |wx.LEFT |wx.RIGHT | wx.TOP, border = 7)
        sizer.Add(tex2, 1 , wx.EXPAND |wx.ALL, border = 7)
        sizer.Add(bold, 0 , wx.TOP |wx.BOTTOM |wx.LEFT, border = 5)        
                
        #Attach sizer to a panel
        panel.SetSizer(sizer)
        panel.Layout()        
        
        self.Center()
        self.Show(True)        
       
        
app = wx.App(redirect=False)
MainWindow(None, -1, 'Toggle Button')
app.MainLoop()

I have made some progress, but still failed to format string in Multiline text box
Help! Help! my friends!

import wx
class MainWindow(wx.Frame):
    def __init__ (self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size= (300, 250))
        
        #add panel
        panel = wx.Panel(self, -1)
        #define sizer
        sizer = wx.BoxSizer(wx.VERTICAL)
        #Add text control
        self.tex1 = wx.TextCtrl(panel, -1)
        shift = wx.Button(panel, -1, "Transfer")
        #Add two to Hsizer
        hsizer = wx.BoxSizer()
        hsizer.Add(self.tex1, 1, wx.EXPAND)
        hsizer.Add(shift, 0, wx.EXPAND |wx.LEFT | wx.RIGHT, 8)
        self.tex2 = wx.TextCtrl(panel, -1, style = wx.TE_MULTILINE)
        bold = wx.Button(panel, -1, "Bold")
        
        #Define button events
        self.Bind(wx.EVT_BUTTON, self.OnTransfer, id=shift.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnBold, id = bold.GetId())
        
        #Add  to sizers
        sizer.Add(hsizer, 0 , wx.EXPAND |wx.LEFT |wx.RIGHT | wx.TOP, border = 7)
        sizer.Add(self.tex2, 1 , wx.EXPAND |wx.ALL, border = 7)
        sizer.Add(bold, 0 , wx.TOP |wx.BOTTOM |wx.LEFT, border = 5)        
                
        #Attach sizer to a panel
        panel.SetSizer(sizer)
        panel.Layout()

        
        
        self.Center()
        self.Show(True)
        
        #Start handlers
    def OnTransfer(self, event):
        text = self.tex1.GetValue()
        self.tex2.SetValue(text)
        self.tex1.SetValue("")
        self.tex1.SetFocus()
        
    def OnBold(self, event):
        text = self.tex2.GetSelection()        
        a=0
        b=0
        a = text[0]
        b = text[1] 
        #How to use them to frormat (bold, italic,...etc) selected text???
  
        
        
        
        
        
        
app = wx.App(redirect=False)
MainWindow(None, -1, 'Toggle Button')
app.MainLoop()

What do you want re-arranged. That piece of code looks perfect.

I want to rearrange so that only selected text is formatted when Bold button is pressed (see my above example). I want to integrate the vegaseat example to work with my GUI above. That is all I want

This is from the RichTextCtrl demo in the wx Docs and Demos package.

self.rtc = rt.RichTextCtrl(self, style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER);
        wx.CallAfter(self.rtc.SetFocus)

        self.rtc.Freeze()
        self.rtc.BeginSuppressUndo()

        self.rtc.BeginParagraphSpacing(0, 20)

        self.rtc.BeginAlignment(rt.TEXT_ALIGNMENT_CENTRE)
        self.rtc.BeginBold()

        self.rtc.BeginFontSize(14)
        self.rtc.WriteText("Welcome to wxRichTextCtrl, a wxWidgets control for editing and presenting styled text and images")
        self.rtc.EndFontSize()
        self.rtc.Newline()

        self.rtc.BeginItalic()
        self.rtc.WriteText("by Julian Smart")
        self.rtc.EndItalic()

        self.rtc.EndBold()

        self.rtc.Newline()

The code is too large to post here, but if you just look into RichTextCtrl you'll find your answers.

Here's the code for the and other formatting options on the toolbar/menubar:

def OnBold(self, evt):
        self.rtc.ApplyBoldToSelection()
        
    def OnItalic(self, evt): 
        self.rtc.ApplyItalicToSelection()
        
    def OnUnderline(self, evt):
        self.rtc.ApplyUnderlineToSelection()
        
    def OnAlignLeft(self, evt):
        self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_LEFT)
        
    def OnAlignRight(self, evt):
        self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_RIGHT)
        
    def OnAlignCenter(self, evt):
        self.rtc.ApplyAlignmentToSelection(rt.TEXT_ALIGNMENT_CENTRE)

Thanks jlm699 .
I should do th rest Now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.