![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
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""" |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
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.
__________________
[ADM]art |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
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.
__________________
[ADM]art |
|
|
|
|
|
#5 |
|
Programming Guru
![]() |
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. |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Sep 2005
Location: Anchorage, Alaska
Posts: 37
Rep Power: 0
![]() |
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
![]()
__________________
[ADM]art |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| C# corruption!!! | Kilo | C++ | 32 | May 21st, 2006 8:44 PM |
| Masm | rsnd | Assembly | 4 | May 20th, 2006 9:05 PM |
| From C syntax to C++ syntax | Navid | C++ | 13 | Jan 15th, 2006 7:42 AM |
| Cobra | Sane | Python | 18 | Aug 22nd, 2005 5:19 PM |
| syntax problems. | troubleshooter | Bash / Shell Scripting | 2 | Apr 5th, 2005 9:34 PM |