Frage

I must be googling using the wrong terms because I can not find what I am looking for. Any help appreciated.

How do I write to a specific line within a textctrl?

Currently my program processes files, when processed the files are listed within the text ctrl. What I would like to achieve is this. List files within textctrl with the word processing after it's name. When processed I would need to re-write to that exact same position but this time replacing the word processing with the word done. I also need to remember which file is printed onto which line. I'm threading so files will not necessarily be finished in the order the were opened, due to their varying sizes.

Thanks for any help!

# This function opens files for processing.
def Encrypt(self, event):
    """
    Runs the thread
    """
    self.file2process = ""
    dlg = wx.FileDialog(self, "Select files", self.file2process, "", "*.*", wx.OPEN |wx.MULTIPLE | wx.CHANGE_DIR)

    if dlg.ShowModal() == wx.ID_OK:
        self.file2process = dlg.GetPaths()

    for fname in self.file2process:
        EncryptThread(fname)

# This is one of two functions that I would need to modify but same prociple would   apply to both so only including this one.

def run(self):
    """Run Worker Thread."""
    # This is the code executing in the new thread.

    keys = {char: chr(i) for i, char in enumerate(self.key)}

    with open(self.fname,'r') as f:
        with open(self.fname + '.tmp', 'w') as temp:        
            for data in f:
                match = ''.join([keys[char] for char in data if char in keys])
                temp.write(match)

    os.remove(self.fname)
    os.rename(self.fname + '.tmp', self.fname)

    msg = self.fname
    wx.CallAfter(Publisher().sendMessage, "update", msg)

# This function updates the textctrl.
def updateDisplay(self, msg):
    """
    Receives data from thread and updates the display
    """
    data = msg.data + "\n"
    self.updateText.WriteText(data)
War es hilfreich?

Lösung

It sounds like you are confusing two different processes: one which writes to TextCtrl, and one which saves and manipulates strings. You should separate out these two processes in GUI design, for your own sanity.

My suggestion would be to save a list of files processed to a list at the same time as you append the file to the TextCtrl. You can then do manipulations on that list very easily using list comprehensions and the re module. All this added manipulation will keep the text in your TextCtrl widget undisturbed, the way the wxPython gods intended.

I can provide some clearer examples if you can provide some code that shows what you are doing.

Andere Tipps

Text controls are organised by a character buffer that may include new lines rather than on a line addressable basis - either use a wx.Grid control or since you need to remember the name positions also remember the char count at that position - I would recommend making wait and done flags the same length.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top