Inheritance problem. Creating an instance.

N

.nu

#!/usr/bin/env python
# -*- coding: utf-8 -*-


# Name: Sleepy Hollow
# Author: .nu

import wx
import os
import sys
NEW_ID = 1; OPEN_ID = 2; SAVE_ID = 3; SAVE_AS_ID = 4;
QUIT_ID = 5; UNDO_ID = 6; REDO_ID = 7; HELPME_ID = 8;
ABOUT_ID = 9; OPTIONS_ID = 10

APP_NAME = 'Sleepy Hollow'

class SleepyHollow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600, 400))
self.SetTitle(APP_NAME)
# create sizers
self.vbox = wx.BoxSizer(wx.VERTICAL)

# create instance of the NoteBook class, then put a tab on it
self.notebook = NoteBook(self)
self.notebook.createTab()

# Create items(statusbar, number panel, etc)
self.statusbar = self.CreateStatusBar()
#self.statusbar.Hide()

#wx.EVT_KEY_UP(self.textarea, self.key_press)


# Call methods that can be turned on/off through options
self.statusbar_toggle(self, 0)


#self.hbox.Add(self.textarea, 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
#self.vbox.Add(self.hbox, 1, wx.EXPAND, 0)

#self.SetAutoLayout(True)

self.Layout()

## Menu
self.menubar = wx.MenuBar()
self.SetMenuBar(self.menubar)

self.file = wx.Menu()
self.new = wx.MenuItem(self.file, NEW_ID, 'new')
self.file.AppendItem(self.new)
self.open = wx.MenuItem(self.file, OPEN_ID, '&Open\tCtrl+O')
self.file.AppendItem(self.open)
self.file.AppendSeparator()
self.save = wx.MenuItem(self.file, SAVE_ID, '&Save\tCtrl+S')
self.file.AppendItem(self.save)
self.saveas = wx.MenuItem(self.file, SAVE_AS_ID, '&Save
as\tCtrl+Shift+s')
self.file.AppendItem(self.saveas)
self.file.AppendSeparator()
self.quit = wx.MenuItem(self.file, QUIT_ID, '&Quit')
self.file.AppendItem(self.quit)

self.edit = wx.Menu()
self.undo = wx.MenuItem(self.edit, UNDO_ID, '&Undo\tCtrl+z')
self.edit.AppendItem(self.undo)
self.redo = wx.MenuItem(self.edit, REDO_ID, '&Redo\tCtrl+Shift+z')
self.edit.AppendItem(self.redo)
self.edit.AppendSeparator()
self.options = wx.MenuItem(self.edit, OPTIONS_ID, '&Options\tCtrl+p')
self.edit.AppendItem(self.options)

self.help = wx.Menu()
self.helpme = wx.MenuItem(self.help, HELPME_ID, '&Help\tF1')
self.help.AppendItem(self.helpme)
self.about = wx.MenuItem(self.help, ABOUT_ID, '&About\tF2')
self.help.AppendItem(self.about)

self.menubar.Append(self.file, '&File')
self.menubar.Append(self.edit, '&Edit')
self.menubar.Append(self.help, '&Help')

# bind items to functions
self.Bind(wx.EVT_MENU, self.New, id=NEW_ID)
self.Bind(wx.EVT_MENU, self.Open, id=OPEN_ID)
self.Bind(wx.EVT_MENU, self.Save, id=SAVE_ID)
self.Bind(wx.EVT_MENU, self.Saveas, id=SAVE_AS_ID)
self.Bind(wx.EVT_MENU, self.Undo, id=UNDO_ID)
self.Bind(wx.EVT_MENU, self.Redo, id=REDO_ID)
self.Bind(wx.EVT_MENU, self.Helpme, id=HELPME_ID)
self.Bind(wx.EVT_MENU, self.About, id=ABOUT_ID)
self.Bind(wx.EVT_MENU, self.Quit, id=QUIT_ID)
self.Bind(wx.EVT_MENU, self.Options, id=OPTIONS_ID)


## Random variables
self.fileName = ''
self.dirName = ''

self.tempFileName = ''
self.tempDirName = ''

self.oldPos = -1
self.show_position(self)


## Menu Methods
def New(self, event):
self.popup(self, "Would you like to save this file before starting a
new one?", "Save?", wx.YES | wx.NO | wx.ICON_QUESTION)
#self.textarea.SetValue('')
self.notebook.createTab()

def Open(self, event):
open_dialog = wx.FileDialog(self, "Open", self.dirName,
self.fileName, "Text Files (*.txt)|*.txt|All Files|*.*", wx.OPEN)
# If open_dialog opens, try to get dirName and fileName, and open it
in the textarea
if (open_dialog.ShowModal() == wx.ID_OK):
try:
self.tempDirName = open_dialog.GetDirectory()
self.tempFileName = open_dialog.GetFilename()
self.file = file(os.path.join(self.tempDirName, self.tempFileName),
'r')
self.fileContents = self.file.read()
self.textarea.SetValue(self.fileContents.decode('utf-8'))
self.SetTitle(APP_NAME + " | " + self.tempFileName + "")
self.fileName = self.tempFileName
print '--------- Open succeeded. Showing temporary and current
filenames (testing purposes) -------';
print '--------- Open succeeded. Showing temporary and current
filenames (testing purposes) -------';
print 'fileName :',self.fileName
self.dirName = self.tempDirName
self.file.close()
except:
self.tempDirName = self.dirName
self.tempFilename = self.fileName
print '--------- open failed. showing file names again (testing
purposes) ----------'
print 'temp file :',self.tempFileName
print 'fileName :', self.fileName
self.popup(self,'Could Not open file.', 'Error', wx.OK
|wx.ICON_ERROR)
open_dialog.Destroy()


def Save(self, event):
if (self.fileName != "") and (self.dirName != ""):
self.file = file(os.path.join(self.dirName, self.fileName), 'w') #
<-- Same as: self.file = file(self.dirName + '/' + self.fileName, 'w')
self.fileContents = self.textarea.GetValue()
self.file.write(self.fileContents.encode('utf-8'))
self.SetTitle(APP_NAME + " | " + self.fileName + "")
self.file.close()
print '*saved*'

else:
self.Saveas(self)


def Saveas(self, event):
# Ask for filename
saveDialogue = wx.FileDialog(self, "Save As", self.dirName,
self.fileName, "Text Files (*.txt)|*.txt|All Files|*.*", wx.SAVE |
wx.OVERWRITE_PROMPT)
if (saveDialogue.ShowModal() == wx.ID_OK):
self.fileName = saveDialogue.GetFilename()
self.dirName = saveDialogue.GetDirectory()
if self.Save(self):
self.SetTitle(APP_NAME + " | " + self.fileName + "")
saveDialogue.Destroy()


def Quit(self, event):
self.Destroy()

def Undo(self, event):
pass

def Redo(self, event):
pass

def Helpme(self, event):
self.popup(self, "No help file has been created yet.", "Help", wx.OK
| wx.ICON_INFORMATION)

def About(self, event):
self.popup(self, "Milkpad Lite 1.0\nby .nu\nwww.ficti0n.com",
"About", wx.OK | wx.ICON_INFORMATION)

def Options(self, event):
pass



## Option Methods
def statusbar_toggle(self, event, switch):
#if 1, then show. if 0, hide.
if (switch == True):
self.statusbar.Show()
self.Refresh()
elif (switch == False):
self.statusbar.Hide()





## Random Methods
def popup(self, event, message, title, button_type_and_icon):
# Show a pop-up message
popup_dialog = wx.MessageDialog(self, message, title,
button_type_and_icon)
popup_dialog.ShowModal()
popup_dialog.Destroy()

def key_press(self, event):
self.show_position(self)
event.Skip()

def show_position(self, event):
#(bPos,ePos) = self.textarea.GetSelection()
#if (self.oldPos != ePos):
#(c,r) = self.textarea.PositionToXY(ePos)
#self.SetStatusText(" " + str((r+1,c+1)))
#self.oldPos = ePos
pass


## This is the Notebook class. It is called in SleepyHollow's __init__
method to
## Create the notebook and createTab() creates/add a new tab(file->new)
class NoteBook(wx.Notebook):
def __init__(self, parent):
wx.Notebook.__init__(self, parent, -1, style=wx.NB_TOP)
#print parent
#self.notebook = wx.Notebook(parent, -1, style=wx.NB_TOP)
self.number_of_tabs = 0

""" #*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*___ERROR__*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_
# This is where I am having problems. I am trying to create an
instance of SleepyHollow
# So i can call its 'popup' method. But i keep getting errors when
trying to create this instance
sh = SleepyHollow(parent, -1, '')
#*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*___ERROR__*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_
"""

def tabCounter(self, action):
""" action is either 'add' or 'subtract' """
if(action == 'add'):
self.number_of_tabs += 1
print self.number_of_tabs
elif(action == 'subtract'):
self.number_of_tabs -= 1
#SleepyHollow.popup(SleepyHollow,'Could Not open file.', 'Error',
wx.OK |wx.ICON_ERROR)

def createTab(self):
#self.notebook_pane1 = wx.Panel(self.notebook)
#wx.Panel.__init__(self)
#t = wx.StaticText(self, -1, "This is a PageOne object", (20,20))
self.textarea = wx.TextCtrl(self, -1, ' hehe', style=wx.TE_MULTILINE)
self.AddPage(self.textarea, 'tab')
self.tabCounter('add')

def deleteTab(self):
pass



if __name__ == "__main__":
app = wx.App(0)
MainFrame = SleepyHollow(None, -1, '')
MainFrame.Show()
app.MainLoop()




So there i have two classes.
The main class, SleepyHollow, creates the main frame and the menu.
Inside the SleepyHollow class, i create an instance to NoteBook class
and call on it.
The NoteBook class creates a notebook and adds tabs to the notebook.

However, inside the NoteBook class, i try to call a SleepyHollow method
(the 'popup' method, for displaying dialogues), but i keep getting this
error:

/usr/bin/python -u "path/to/file/file.py"
Traceback (most recent call last):

File "path/to/file/file.py", line 256, in ?
MainFrame = SleepyHollow(None, -1, '')

File "path/to/file/file.py", line 26, in __init__
self.notebook = NoteBook(self)

File "path/to/file/file.py", line 222, in __init__
wx.Notebook.__init__(self, parent, -1, style=wx.NB_TOP)

File "path/to/file/file.py", line 3117, in __init__
self.this = newobj.this
AttributeError: 'PySwigObject' object has no attribute 'this'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top