Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   wxPython syntax error help. (http://www.programmingforums.org/showthread.php?t=10850)

Yarvin Jul 26th, 2006 7:12 PM

wxPython syntax error help.
 
I'm following the getting started tutorial here and when I try adding the OnOpen method I get this syntax error:
:

: python partTwo.py
  File "partTwo.py", line 15
    filemenu.Append(ID_OPEN, '&Open', 'Open a file')
    ^
SyntaxError: invalid syntax
:


here is the full source code.

:

import os
import wx
ID_ABOUT=101
ID_EXIT=110
ID_OPEN=111
class MainWindow(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,wx.ID_ANY, title, size = ( 200,100),
                                        style=wx.DEFAULT_FRAME_STYLE|
                                        wx.NO_FULL_REPAINT_ON_RESIZE)
        self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
        self.CreateStatusBar() # A StatusBar in the bottom of the window
        # Setting up the menu.
        filemenu= wx.Menu()
                filemenu.Append(ID_OPEN, '&Open', 'Open a file')
        filemenu.Append(ID_ABOUT, "&About"," Information about this program")
        filemenu.AppendSeparator()
        filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.
        wx.EVT_MENU(self, ID_ABOUT, self.OnAbout) # attach the menu-event ID_ABOUT to the
                                                          # method self.OnAbout
        wx.EVT_MENU(self, ID_EXIT, self.OnExit)  # attach the menu-event ID_EXIT to the
                                                          # method self.OnExit
                wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
        self.Show(True)
    def OnAbout(self,e):
        d= wx.MessageDialog( self, " A sample editor \n"
                            " in wxPython","About Sample Editor", wx.OK)
                            # Create a message dialog box
        d.ShowModal() # Shows it
        d.Destroy() # finally destroy it when finished.
    def OnExit(self,e):
        self.Close(True)  # Close the frame.
    def OnOpen(self,e):
        """ Open a file"""
        self.dirname = ''
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename=dlg.GetFilename()
            self.dirname=dlg.GetDirectory()
            f=open(os.path.join(self.dirname,self.filename),'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()
app = wx.PySimpleApp()
frame = MainWindow(None, -1, "Sample editor")
app.MainLoop()


I'm running this on Mac OS 10.4.7 with Python 2.4.3 and wxPython 2.6.

any and all help is very much appreciated :) thank you.

Alvin

Sane Jul 26th, 2006 7:20 PM

In Python, indentation matters. You must keep the level of all indented blocks the same. You have some lines that are randomly bumped out of place.

Also, you can not add multiple strings together by merely placing one beneath an other. You need to use triple quotes """ to enclose multi-line text.

:

"""this is the first line
this is the second line"""


Yarvin Jul 26th, 2006 10:12 PM

the indent is a mistake in my posting it to the forum. this is how the code looks on my screen: image

as for the comments. thats how the comments were on the tutorial, I never touched them. all I'm trying to do is add the OnOpen method as an excerise.

Yarvin Jul 26th, 2006 10:15 PM

ok I figured it out! it was the text editor that I'm using. when I opened it in vim I saw the lines that were out of place.

thanks for the help.

Sane Jul 26th, 2006 10:29 PM

Oh, wow. That multiline literal worked? I never knew you could concact them by simply starting a new literal on the next line... heh.

Glad you figured it out.

Yarvin Jul 26th, 2006 10:31 PM

well yeah... with the multi-line literal I can see the whole line if code without having to adjust the size of my text editor... it really helps :D


All times are GMT -5. The time now is 12:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC