How can I use modules from the Py package in my wxPython programs

What do you do if you don't want an entire PyCrust frame in your application? What if you just want the shell interface in one frame, and perhaps a namespace viewer in another? And what if you want them to be permanent additions to your program? These alternatives are not only possible, they're also fairly easy. We'll end this chapter with one example of how this can be done.

We're going to revisit the program we created in chapter 2, the one with a menubar, toolbar, and status bar. We'll add another menu with one item that

How can I use modules from the Py package 113

in my wxPython programs?

How can I use modules from the Py package 113

in my wxPython programs?

Pyqt Tree Examples
Figure 4.11 Foundation program with shell and filling frames

displays a shell frame, and another item that displays a filling frame. Finally, we'll set the root of the filling tree to the frame object from our main program. The results are displayed in figure 4.11.

Listing 4.7 shows the modified source code. (Refer to chapter 2 for an explanation of the original program.) As you can see, only a couple of extra lines of code were used to add the ability to launch a shell frame and a filling frame, with each operating on the primary application frame.

Listing 4.7 Foundation program with additional runtime tools

#!/usr/bin/env python import wx from import ShellFrame from import FillingFrame

Importing the frame classes import images class ToolbarFrame(wx.Frame):

wx.Frame._init_(self, parent, id, 'Toolbars', size=(3 00, 200)) panel = wx.Panel(self, -1) panel.SetBackgroundColour('White') statusBar = self.CreateStatusBar() toolbar = self.CreateToolBar()

toolbar.AddSimpleTool(wx.NewId(), images.getNewBitmap(),

"New", "Long help for 'New'") toolbar.Realize() menuBar = wx.MenuBar() menul = wx.Menu() menuBar.Append(menu1, "&File") menu2 = wx.Menu()

menu2.Append(wx.NewId(), "&Copy", "Copy in status bar") menu2.Append(wx.NewId(), "C&ut", "") menu2.Append(wx.NewId(), "Paste", "") menu2.AppendSeparator()

menu2.Append(wx.NewId(), "^Options...", "Display Options") menuBar.Append(menu2, "&Edit")

shell = menu3.Append(-1, "&wxPython shell",

"Open wxPython shell frame") filling = menu3.Append(-1, "&Namespace viewer",

"Open namespace viewer frame") menuBar.Append(menu3, "&Debug") self.Bind(wx.EVT_MENU, self.OnShell, shell) self.Bind(wx.EVT_MENU, self.OnFilling, filling)

Creating the Debug menu and items

Setting the menu event handlers self.SetMenuBar(menuBar)

def OnCloseMe(self, event): self.Close(True)

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

def OnShell(self, event):

frame = ShellFrame(parent=self) frame.Show()

def OnFilling(self, event):

frame = FillingFrame(parent=self) frame.Show()

The OnShell menu item handler

The OnFilling menu item handler if

app = wx.PySimpleApp()

app.frame = ToolbarFrame(parent=None, id=-1)



O Here we import the ShellFrame and FillingFrame classes.

C As with the previous two menus, we append items to our third menu, the Debug menu, and append it to the frame's menubar.

d Binding a function to wx.EVT_MENU() allows us to associate a handler with a menu item, so that when the menu item is selected the handler is called.

O When the user selects Python shell from the Debug menu, a shell frame is created whose parent is the toolbar frame. When the toolbar frame is closed, any open shell or filling frames is also closed.

Was this article helpful?

0 0

Post a comment