File: Notebook.1.py

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (74 lines) | stat: -rw-r--r-- 2,476 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
##Andrea Gavana
#!/usr/bin/env python

# This sample creates a "File->Open" menu, and lets the user select
# a Python file. Upon selection, the file is read in memory and a new
# wx.TextCtrl showing the file content is added as a page of a wx.Notebook

import wx
import os

class NotebookFrame(wx.Frame):

    def __init__(self, parent, title):

        wx.Frame.__init__(self, parent, title=title)

        # Create the notebook
        self.notebook = wx.Notebook(self, style=wx.NB_BOTTOM)

        # Setting up the menu
        file_menu = wx.Menu()

        # wx.ID_OPEN
        menu_item = file_menu.Append(wx.ID_OPEN, '&Open...', 'Open and read a new Python file')
        # Bind the "select menu item" event to the OnOpen event handler
        self.Bind(wx.EVT_MENU, self.OnOpen, menu_item)

        # Creating the menubar
        menu_bar = wx.MenuBar()

        # Adding the 'file_menu' to the menu bar
        menu_bar.Append(file_menu, '&File')

        # Adding the menu bar to the frame content
        self.SetMenuBar(menu_bar)

        self.Show()


    def OnOpen(self, event):

        # This is how you pre-establish a file filter so that the dialog
        # only shows the extension(s) you want it to.
        wildcard = 'Python source (*.py)|*.py'

        dlg = wx.FileDialog(None, message="Choose a Python file", defaultDir=os.getcwd(),
                            defaultFile="", wildcard=wildcard, style=wx.FD_OPEN)

        # Show the dialog and retrieve the user response. If it is the OK response,
        # process the data.
        if dlg.ShowModal() == wx.ID_OK:
            # This returns the file that was selected
            path = dlg.GetPath()

            # Open the file as read-only and slurp its content
            with open(path, 'rt') as fid:
                text = fid.read()

            # Create the notebook page as a wx.TextCtrl and
            # add it as a page of the wx.Notebook
            text_ctrl = wx.TextCtrl(self.notebook, style=wx.TE_MULTILINE)
            text_ctrl.SetValue(text)

            filename = os.path.split(os.path.splitext(path)[0])[1]
            self.notebook.AddPage(text_ctrl, filename, select=True)

        # Destroy the dialog. Don't do this until you are done with it!
        # BAD things can happen otherwise!
        dlg.Destroy()


app = wx.App(False)
frame = NotebookFrame(None, 'Notebook example')
app.MainLoop()