wxPython probleme

Cliff Wells LogiplexSoftware at earthlink.net
Mon Nov 25 13:23:45 EST 2002


On Sat, 2002-11-23 at 06:13, Jonas Geiregat wrote:
> I have 2 questions
> 1.the lines I've commented out don't work and I don't see the probleme

Seems to work fine here (select File->Exit, program exits - or were you
expecting something else?).  Could you be more specific than "doesn't
work"?.  Do you have a problem with your indentation?

> 2.I want to create a function so I can save what I typed in in a file I 
> know how to write to a file but I don't know what variable the 
> wxTextCtrl uses in other words what var do I have to write to the file ?

Use wxTextCtrl.GetValue() (see below).


> here is the code:

I've made several minor modifications to your source.  Note the use of
wxNewId() rather than explicit id's.  It's better to let wxPython manage
the id's that your program uses.  Far less prone to error.  I also got
sort of carried away and did some other stuff ;)  Anyway, I this seems
to work for me:

import sys, os
from   wxPython.wx import *

ID_OPEN = wxNewId()
ID_SAVE = wxNewId()
ID_EXIT = wxNewId()

class main_window(wxFrame):
    def __init__(self, parent, id, title):
        wxFrame.__init__(self, parent, -1, title, size = (500,500),
                        
style=wxDEFAULT_FRAME_STYLE|wxNO_FULL_REPAINT_ON_RESIZE)
            
        self.CreateStatusBar()
        self.SetStatusText("small editor")
        
        M_file = wxMenu()
        M_file.Append(ID_OPEN, "&Open","Open a file")
        M_file.Append(ID_SAVE, "&Save","Save a file")
        M_file.Append(ID_EXIT, "&Quit","Quit this small editor")
        
        menuBar = wxMenuBar()
        menuBar.Append(M_file,"&File")
        self.SetMenuBar(menuBar)

        self.path = None
        self.control = wxTextCtrl(self, -1, style=wxTE_MULTILINE)
        self.Show(true)
        
        EVT_MENU(self, ID_EXIT,  self.OnExit)
        EVT_MENU(self, ID_SAVE,  self.OnSave)
        
    def OnExit(self, event):
        self.Close(true)
                                
    def OnSave(self, event):
        text = self.control.GetValue()
        if self.path is None:
            wildcard = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
            dlg = wxFileDialog(self, "Save file as...", "", "",
wildcard, wxSAVE)
            if dlg.ShowModal() == wxID_OK:
                path = dlg.GetPath()
            dlg.Destroy() 

            if os.path.exists(path):
                dlg = wxMessageDialog(self, 'Overwrite %s?' % path,
                                      'Confirm overwrite file',
                                      wxYES_NO | wxNO_DEFAULT |
wxICON_QUESTION)
                response = dlg.ShowModal()
                dlg.Destroy()
                if response != wxID_YES:
                    return
            self.path = path
            
        f = file(self.path, 'w')
        f.write(text)
        f.close()
        
                        
class App(wxApp):
    def OnInit(self):
        frame = main_window(None, -1, "wxPython: (A Demonstration)")
        frame.Show(true)
        self.SetTopWindow(frame)
        return true

app = App(0)
app.MainLoop()




-- 
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308  (800) 735-0555 x308
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20021125/6f307fd7/attachment.sig>


More information about the Python-list mailing list