i am working on a project but this parts seems to stall me
i want to be able to write to a text file to change just two lines of d file say line 1 and 3 using wxwidgets
i will appreciate helps nd suggestions thanks

3 Years
Discussion Span
Last Post by dreking6
Featured Replies
  • What have you tried so far? Please show us the existing code, if any. Note that, chances are, you are going to have to rad the whole file into memory (or at least the part from where you are editing it onwards) and write it back to the file. You … Read More


What have you tried so far? Please show us the existing code, if any.

Note that, chances are, you are going to have to rad the whole file into memory (or at least the part from where you are editing it onwards) and write it back to the file. You may want to use an intermediate file, and then delete or rename the original file and change the name of the intermediate file to that of the original. Something like this:

read the lines in from 'original' up to the mark where you are changing it
write that first section out to 'intermediate'
write your changed text to 'intermediate'
read in the part of 'original' following the changed text
write that out to 'intermediate'
rename 'original' as 'original.old' or something similar
rename the 'intermediate' to 'original'

You would, of course, use the appropriate names for the files, rather than 'original' and 'intermediate'. You would want to look up the os.rename() method to get the details of renaming files.

I would add that the issue of file handling is completely independent of the windowing library, and it would probably be wise to keep the code for the text file change separate from the user interface. Write the function for edtining the file as if you didn't know where the text to change came from; pass the data as an argument to it, rather than getting it from the user directly. You'll find it easier both to write the function and debug it this way.

Edited by Schol-R-LEA


the problem is i am actually not soo good with wxpython as i learnt only tkinter..but the source i m improving is written in wxython which is really giving me headache a it is complex

Edited by dreking6


This example might help ...

''' pwx_Simple_editor1.py
the start of a small text editor with a toolbar and image icons
from the wx.ArtProvider's wx.ART_TOOLBAR selection

notice that the wx.TextCtrl() surface has already some advanced
features: select text, right click to cut, copy and paste etc.
tested with Python27 and  wxPython 2.9.1
also Python33 and wxPython_Phoenix 3.0.0

import os
import wx

class MyFrame(wx.Frame):
    def __init__(self, title):
        wx.Frame.__init__(self, None, wx.ID_ANY, title, size=(500, 300))
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)

        # create a StatusBar at the bottom of the window
        self.SetStatusText(" Click on a toolbar icon")

        # create a ToolBar at the top of the window
        toolbar = wx.ToolBar(self, -1, style=wx.TB_HORIZONTAL|wx.NO_BORDER)
        toolbar.AddSimpleTool(wx.ID_OPEN, self.getBMP(wx.ART_FILE_OPEN),
            "Load", " Load a text file")
        toolbar.AddSimpleTool(wx.ID_SAVE, self.getBMP(wx.ART_FILE_SAVE),
            "Save", " Save the text file")
        toolbar.AddSimpleTool(wx.ID_ABOUT, self.getBMP(wx.ART_INFORMATION),
            "About"," About message")
        toolbar.AddSimpleTool(wx.ID_EXIT, self.getBMP(wx.ART_QUIT),
            "Exit"," Exit the program")

        # bind the various toolbar icon click events to an action
        self.Bind(wx.EVT_TOOL, self.onLoad, id=wx.ID_OPEN)
        self.Bind(wx.EVT_TOOL, self.onSave, id=wx.ID_SAVE)
        self.Bind(wx.EVT_TOOL, self.onAbout, id=wx.ID_ABOUT)
        self.Bind(wx.EVT_TOOL, self.onExit, id=wx.ID_EXIT)

    def getBMP(self, pic_id):
        """get a bitmap image from the wxPython art provider"""
        size = wx.Size(24, 24)
        return wx.ArtProvider.GetBitmap(pic_id, wx.ART_TOOLBAR, size)

    def onAbout(self, e):
        """ the about box """
        about = wx.MessageDialog( self, " A very simple text editor \n"
            " using the wxPython GUI toolkit", "About Simple Editor", wx.OK)

    def onLoad(self, e):
        """ load a file"""
        dirname = ''
        filename = ''
        mask = "Text (.txt)|*.txt|All (.*)|*.*"
        dlg = wx.FileDialog(self, "Choose a file to load",
            dirname, filename, mask, wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            f = open(path,'r')

    def onSave(self, e):
        """ save a file"""
        dirname = ''
        filename = ''
        mask = "Text (.txt)|*.txt|All (.*)|*.*"
        dlg = wx.FileDialog(self, "Choose or create a file to save to",
            dirname, filename, mask, wx.SAVE)
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPath()
            f = open(path,'w')

    def onExit(self, e):

app = wx.App(0)
# create an instance of MyFrame and show it
MyFrame(title="A Simple Editor with a toolbar").Show()

Edited by vegaseat


thanks so much though the project as stalled now after i discovered some few bugs i am finding it hard to rectify i will be glad if i could get someone who can point and debug the program for me

This question has already been answered. Start a new discussion instead.
Be sure to adhere to our posting rules.