473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Inheritance problem. Creating an instance.

.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('u tf-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

""" #*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*___ERRO R__*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_ *_*_*__*_*_*_*_*_
# 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, '')
#*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*___ERRO R__*__*_*_*_*_*_*_*_*__*_*_*_*_*_*_*_*__*_*_*_*_*_ *_*_*__*_*_*_*_*_
"""

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'

Dec 6 '06 #1
0 1305

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Alex Vinokur | last post by:
Hi, I need something like "function inheritance". typedef void (*func_type1) (int); typedef int (*func_type2) (double); typedef char (*func_type3) (short, int); .... I need a vector...
12
by: Taylor | last post by:
I'm trying to understand inheritance. I'd like to make my own type of IPAddress lets call it myIp. The following gives me CS0029 error: Cannot implicitly convert type 'System.Net.IPAddress' to...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
9
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
9
by: glomde | last post by:
Hi I wonder if you can set what subclass a class should have at instance creation. The problem is that I have something like: class CoreLang(): def AssignVar(self, var, value): pass class...
5
by: Michael Thompson | last post by:
I am new to .Net and OOP techniques; I am not even certain that I using the correct terminology here. I believe that I want to know how to do inheritance. I want to create a custom "string"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.