Reading a Story with wxNotebook (Python)

vegaseat 0 Tallied Votes 295 Views Share

The wxNotebook method from the wxPython module allows you to show a lot of data in a limited window space. It brings up tabbed pages you can click on to open each page. Enough said, run the code and experience the possibilities this humble method gives you.

# experiments with wxNotebook
# make sure you have wxPython installed 
# for instance install with wxPython2.5-win32-unicode-2.5.4.1-py24.exe
# from http://prdownloads.sourceforge.net/wxpython/
# tested with Python24   vegaseat   18jul2005

import wx

class Panel(wx.Panel):
  """ class Panel creates a panel with a multiline edit box on it, inherits wx.Panel """
  def __init__(self, parent, id, txt, bgcolor):
    # create a panel
    wx.Panel.__init__(self, parent, id)
    self.SetBackgroundColour(bgcolor)
    self.edit1 = wx.TextCtrl(self, id, value=txt, pos=(10,10), size=(360,200), style=wx.TE_MULTILINE)
    self.edit1.SetBackgroundColour((240, 230, 220))  # modified wheat color
    self.edit1.SetFocus()


# tell a story in three pages ...
txt1 = """A priest was talking to a nun, and he saw that
her belly was getting noticeably big, and
he made a comment about it.  She replied to
him that it was just a little gas.
"""

txt2 = """A couple months later, he ran into her again.
This time, her belly was really big. She just
patted her belly and said, "Just a little gas."
"""

txt3 = """Two months went by, and he came across the
nun again, and she was pushing a baby
carriage.  The priest bent down and looked into the
carriage and said, "Cute little fart, isn't he?"
"""
    
app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
frame = wx.Frame(None, -1, "Notebook Pages", size=(400,300))
nb = wx.Notebook(frame, -1)

# create the content of each page
panel1 = Panel(nb, -1, txt1, "yellow")
panel2 = Panel(nb, -1, txt2, "green")
panel3 = Panel(nb, -1, txt3, "red")

# create the notebook pages, add content and title to each page
nb.AddPage(panel1, "Page1")
nb.AddPage(panel2, "Page2")
nb.AddPage(panel3, "Page3")

frame.Show(True)
# start the event loop
app.MainLoop()
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Adopted the newer wxPython coding style, which identifies the namespace wx. The DrPython editor code completion plugin works well that way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.