1

I am new to Python and so i am new to wxPython as well. I was just wondering if there is any difference between these wx.TextCtrl functions. This mini code shows three times the same output. If there is no difference, is there a historic reason for these functions?

import wx

class testUI(wx.Panel):
    textCtrl = ''

    def __init__(self, parent, name):
        super(testUI, self).__init__(parent, name=name)
        self.buildUI()
        self.Show(True)
        self.textCtrl.write('bli\n')
        self.textCtrl.WriteText('bla\n')
        self.textCtrl.AppendText('blub\n')

    def buildUI(self):
        self.textCtrl = wx.TextCtrl(self, style=wx.TE_MULTILINE|wx.TE_READONLY)
        box = wx.BoxSizer(wx.VERTICAL)
        box.Add(self.textCtrl, proportion=1, flag=wx.EXPAND)

def main():
    app = wx.App(False)
    root = wx.Frame(parent=None, title='testUI')
    testUI(parent=root, name='testUI')
    root.Show(True)
    app.MainLoop()

# Standard boilerplate to call the main() function.
if __name__ == '__main__':
    main()

thanks :)

ChrisM
  • 35
  • 1
  • 6

3 Answers3

1

Guessing people may hit this thread for the same reason I did, which is that the "new" version of wxPython (4.0.1) lists write() as a method on the wx.TextCtrl class - but it's not defined. I needed a write function to be able to use the logging.StreamHandler class; because these methods should all do the same thing, I recreated the missing write method with an assignment - this works:

import wx

import logging
lgr = logging.getLogger(__name__)
lgr.setLevel(logging.DEBUG)
fmt = logging.Formatter('%(asctime)s: %(name)s [%(levelname)s] %(message)s')
debug = lgr.debug

class LogTab(wx.Panel):
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.TAB_TRAVERSAL
        wx.Panel.__init__(self, *args, **kwds)
        self.text_log = wx.TextCtrl(self, wx.ID_ANY, "",
                                    style=wx.TE_MULTILINE | wx.TE_READONLY)

        self.text_log.write = self.text_log.WriteText

        self.__set_properties()
        self.__do_layout()
        self.log_handler = logging.StreamHandler(self.text_log) 
        self.log_handler.setFormatter(fmt) 

        lgr.addHandler(self.log_handler)
0

The 3 methods appear to be functionally the same. However, I would argue that you would use AppendText for adding additional text to a text control to make what you're doing very clear in the code itself. Most of the time, you will normally use SetValue. I have used WriteText when redirecting stdout, but that's it. You can read about that use case here:

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0

EDIT:

Ok, instead of reading my entire answer the difference between .AppendText() and .WriteText() appears to be that the first one adds text to the end of the textcontrol and the second one adds it at the current insertion point as explained here.

Old answer (may still be usefull as an example):

Hmm.. strange. I ended up here because using .WriteText() on my wx.stc.StyledTextCtrl() was giving strange results. I now changed it to .write() and it works.

I am using my wx.stc.StyledTextCtrl() as a drag and drop window that lists the dropped filepaths. However, since the window is also editable (I need it to be) the InsertionPoint can be located anywhere.

So, whenever someone drops a new filepath (or multiple filepaths) onto my wx.stc.StyledTextCtrl() I need it to check where the current InsertionPoint is located. If the InsertionPoint is located in a line that already holds text, it needs to be moved to the end of that specific line. From there it needs to print a newline and the received filepath(s).

My code looks like this:

def write_interactive(self, text):

        curPos = self.interactivewindow.GetInsertionPoint()
        lineVal,curCol,curRow = self.interactivewindow.PositionToXY(curPos)
        lineNum = curRow
        lineText = self.interactivewindow.GetLineText(lineNum)

        if len(lineText) > 0:

            endOfLine = self.interactivewindow.XYToPosition(len(lineText), curRow)
            self.interactivewindow.SetInsertionPoint(endOfLine)
            self.interactivewindow.write('\n' + text)

        else:

            self.interactivewindow.write(text)

Now, as to your question, I have noticed that when using self.interactivewindow.WriteText(text) the text that is located between the initial InsertionPoint and the end of that specific line vanishes. And the new text is written over it. However, when using self.interactivewindow.write(text), everything works fine.

Montmons
  • 1,416
  • 13
  • 44