I'm attaching a stripped working version of my script with all
relavant methods so that anyone willing to help me can see
fully what I'm trying to do, and see the errors for themselves.
(First, thank you to Andrea Gavana for replying to my first
message. I had subscribed to the digest, so didn't see the
reply until I started checking the list group's archive online,
and I couldn't reply there.
I'm subscribed to the regular list now, so that I won't have
to wait for the digest to get responses.)
I am dynamically creating notebook pages as I open files
to display their contents in a textCtrl. Then when I "close" the
file (it's already been closed after I do file.readlines() ), I want
to destroy the notebook page created for the file. It almost
works (sigh). Destroying the notebook page, should destroy
the widgets on the page, but I am getting some odd errors.
The program starts with a Menu and statusbar, and an
otherwise empty panel. There is actually a sizer and Notebook
with no pages.
The File/Open triggers event onOpen, which provides
a dialog for selecting a file or files to open, returning paths. Then,
for path in paths
self.onFileOpen(path)
the method onFileOpen,
1. opens the file in read mode,
2. adds the file to filehistory to appear on the menu
3.extracts the basename of the file from the path and extension
to be used as a label.
4. Then creates a page, and adds it to the (empty) notebook
5. Adds a sizer to the page, with a static box for a label
6. Adds a textCtrl to the sizer
7. does the sizing for the widgets
8. then writes the contents of the open file to the textCtrl
9. Closes the file from read mode.
So far, the script seems to work fine. I can open one file at a time,
or several. The first problem I have occurs because of how I
"close" the file, IE destroy the page.
I get one of two results when I destroy a Notebook page that
has 3 widgets on it--which occur when I use the Menu based
exit method, NOT at the point where the Destroy in invoked for
the page. I get either a segfault or the following lines in the stderr:
(python:20463): Gtk-CRITICAL **: gtk_widget_ref: assertion `GTK_IS_WIDGET (widget)' failed
(python:20463): Gtk-CRITICAL **: gtk_widget_unrealize: assertion `GTK_IS_WIDGET (widget)' failed
(python:20463): Gtk-CRITICAL **: gtk_widget_unparent: assertion `GTK_IS_WIDGET (widget)' failed
followed by one of these lines, before returning to the prompt
Illegal instruction
Segmentation fault
Trace/breakpoint trap
The last line varies, after different runs. I know the above is related to Destroying
the page, even though I don't see the errors appearing until I exit the program.
1. I'm not sure how to avoid the errors. I'm going to be adding Try/Except to the script
in several places, but I don't want to use Try/Except to "patch" a broken method
for Detroying the pages. Does anyone have ideas why I'm getting the above errors,
and how they could be avoided?
2. The next thing I have to deal with is that invoking the onFileClose method will only Destroy
the last page created. I think I understand the reason for this -- the onFileOpen method
uses the same name for each Page, Sizer and textCtrl that is created at runtime. I assume
the widget ID must be different, or each succeeding call of the method would just overwrite the
contents of the page textCtrl on the screen, rather than creating multiple (different) pages.
But the self.tab.Destroy() name can only refer to last widget created.
I've anticipated that I need to use wx.NewId and wx.EVT_NOTEBOOK_PAGE_CHANGED
to track the active page, and have Menu/File/Close call the onFileClose method on
the active page, by ID.
There is where I have my third problem: I can't seem to figure out how to GetId.
I've already used wx.NewId when creating the Menu (actually, wxglade did)
I have disabled Menu/File options that I haven't written events for. But only using,
for example, wx.ID_NEW for the File/New option, but not for others that don't have
such constants attached. I'd like to disable the Menu/Editing/Find entry which has
a name, a label, and an ID generated with wx.NewId. I don't want to specifiy specific
IDs, in case the menus change over time. And similarly, I simply can't choose a
specific numerical ID for the pages that are being created dynamically.
3. I just can't seem for figure out how to get or return the unique integer ID of a widget.
I would like to do so, for both menuItems and the Notebook Pages that are created.
I know there is a GetId method or function somewhere, but I couldn't seem to locate
or figure out how to use it when trying to Disable Menu options.
4. Is there another better way to create/destroy dynamic Widgets, that would accomplish
what I am trying to do?
5. Another related question: It seems unlikely, but is there any possble way to create
dynamic variable names? I mean dynamically generate a string that is "imported" or
"applied" to namespace so that it can be used as a variable name? IE
aStringVar = "NewVarName" #so that I could then assign
NewVarName = "aValue"
test.py (7.1 KB)