• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""About Dialog for IDLE
2
3"""
4import os
5import sys
6from platform import python_version, architecture
7
8from tkinter import Toplevel, Frame, Label, Button, PhotoImage
9from tkinter import SUNKEN, TOP, BOTTOM, LEFT, X, BOTH, W, EW, NSEW, E
10
11from idlelib import textview
12
13version = python_version()
14
15
16def build_bits():
17    "Return bits for platform."
18    if sys.platform == 'darwin':
19        return '64' if sys.maxsize > 2**32 else '32'
20    else:
21        return architecture()[0][:2]
22
23
24class AboutDialog(Toplevel):
25    """Modal about dialog for idle
26
27    """
28    def __init__(self, parent, title=None, *, _htest=False, _utest=False):
29        """Create popup, do not return until tk widget destroyed.
30
31        parent - parent of this dialog
32        title - string which is title of popup dialog
33        _htest - bool, change box location when running htest
34        _utest - bool, don't wait_window when running unittest
35        """
36        Toplevel.__init__(self, parent)
37        self.configure(borderwidth=5)
38        # place dialog below parent if running htest
39        self.geometry("+%d+%d" % (
40                        parent.winfo_rootx()+30,
41                        parent.winfo_rooty()+(30 if not _htest else 100)))
42        self.bg = "#bbbbbb"
43        self.fg = "#000000"
44        self.create_widgets()
45        self.resizable(height=False, width=False)
46        self.title(title or
47                   f'About IDLE {version} ({build_bits()} bit)')
48        self.transient(parent)
49        self.grab_set()
50        self.protocol("WM_DELETE_WINDOW", self.ok)
51        self.parent = parent
52        self.button_ok.focus_set()
53        self.bind('<Return>', self.ok)  # dismiss dialog
54        self.bind('<Escape>', self.ok)  # dismiss dialog
55        self._current_textview = None
56        self._utest = _utest
57
58        if not _utest:
59            self.deiconify()
60            self.wait_window()
61
62    def create_widgets(self):
63        frame = Frame(self, borderwidth=2, relief=SUNKEN)
64        frame_buttons = Frame(self)
65        frame_buttons.pack(side=BOTTOM, fill=X)
66        frame.pack(side=TOP, expand=True, fill=BOTH)
67        self.button_ok = Button(frame_buttons, text='Close',
68                                command=self.ok)
69        self.button_ok.pack(padx=5, pady=5)
70
71        frame_background = Frame(frame, bg=self.bg)
72        frame_background.pack(expand=True, fill=BOTH)
73
74        header = Label(frame_background, text='IDLE', fg=self.fg,
75                       bg=self.bg, font=('courier', 24, 'bold'))
76        header.grid(row=0, column=0, sticky=E, padx=10, pady=10)
77
78        tk_patchlevel = self.tk.call('info', 'patchlevel')
79        ext = '.png' if tk_patchlevel >= '8.6' else '.gif'
80        icon = os.path.join(os.path.abspath(os.path.dirname(__file__)),
81                            'Icons', f'idle_48{ext}')
82        self.icon_image = PhotoImage(master=self._root(), file=icon)
83        logo = Label(frame_background, image=self.icon_image, bg=self.bg)
84        logo.grid(row=0, column=0, sticky=W, rowspan=2, padx=10, pady=10)
85
86        byline_text = "Python's Integrated Development\nand Learning Environment" + 5*'\n'
87        byline = Label(frame_background, text=byline_text, justify=LEFT,
88                       fg=self.fg, bg=self.bg)
89        byline.grid(row=2, column=0, sticky=W, columnspan=3, padx=10, pady=5)
90        email = Label(frame_background, text='email:  idle-dev@python.org',
91                      justify=LEFT, fg=self.fg, bg=self.bg)
92        email.grid(row=6, column=0, columnspan=2, sticky=W, padx=10, pady=0)
93        docs = Label(frame_background, text="https://docs.python.org/"
94                     f"{version[:version.rindex('.')]}/library/idle.html",
95                     justify=LEFT, fg=self.fg, bg=self.bg)
96        docs.grid(row=7, column=0, columnspan=2, sticky=W, padx=10, pady=0)
97
98        Frame(frame_background, borderwidth=1, relief=SUNKEN,
99              height=2, bg=self.bg).grid(row=8, column=0, sticky=EW,
100                                         columnspan=3, padx=5, pady=5)
101
102        pyver = Label(frame_background,
103                      text='Python version:  ' + version,
104                      fg=self.fg, bg=self.bg)
105        pyver.grid(row=9, column=0, sticky=W, padx=10, pady=0)
106        tkver = Label(frame_background, text='Tk version:  ' + tk_patchlevel,
107                      fg=self.fg, bg=self.bg)
108        tkver.grid(row=9, column=1, sticky=W, padx=2, pady=0)
109        py_buttons = Frame(frame_background, bg=self.bg)
110        py_buttons.grid(row=10, column=0, columnspan=2, sticky=NSEW)
111        self.py_license = Button(py_buttons, text='License', width=8,
112                                 highlightbackground=self.bg,
113                                 command=self.show_py_license)
114        self.py_license.pack(side=LEFT, padx=10, pady=10)
115        self.py_copyright = Button(py_buttons, text='Copyright', width=8,
116                                   highlightbackground=self.bg,
117                                   command=self.show_py_copyright)
118        self.py_copyright.pack(side=LEFT, padx=10, pady=10)
119        self.py_credits = Button(py_buttons, text='Credits', width=8,
120                                 highlightbackground=self.bg,
121                                 command=self.show_py_credits)
122        self.py_credits.pack(side=LEFT, padx=10, pady=10)
123
124        Frame(frame_background, borderwidth=1, relief=SUNKEN,
125              height=2, bg=self.bg).grid(row=11, column=0, sticky=EW,
126                                         columnspan=3, padx=5, pady=5)
127
128        idlever = Label(frame_background,
129                        text='IDLE version:   ' + version,
130                        fg=self.fg, bg=self.bg)
131        idlever.grid(row=12, column=0, sticky=W, padx=10, pady=0)
132        idle_buttons = Frame(frame_background, bg=self.bg)
133        idle_buttons.grid(row=13, column=0, columnspan=3, sticky=NSEW)
134        self.readme = Button(idle_buttons, text='README', width=8,
135                             highlightbackground=self.bg,
136                             command=self.show_readme)
137        self.readme.pack(side=LEFT, padx=10, pady=10)
138        self.idle_news = Button(idle_buttons, text='NEWS', width=8,
139                                highlightbackground=self.bg,
140                                command=self.show_idle_news)
141        self.idle_news.pack(side=LEFT, padx=10, pady=10)
142        self.idle_credits = Button(idle_buttons, text='Credits', width=8,
143                                   highlightbackground=self.bg,
144                                   command=self.show_idle_credits)
145        self.idle_credits.pack(side=LEFT, padx=10, pady=10)
146
147    # License, copyright, and credits are of type _sitebuiltins._Printer
148    def show_py_license(self):
149        "Handle License button event."
150        self.display_printer_text('About - License', license)
151
152    def show_py_copyright(self):
153        "Handle Copyright button event."
154        self.display_printer_text('About - Copyright', copyright)
155
156    def show_py_credits(self):
157        "Handle Python Credits button event."
158        self.display_printer_text('About - Python Credits', credits)
159
160    # Encode CREDITS.txt to utf-8 for proper version of Loewis.
161    # Specify others as ascii until need utf-8, so catch errors.
162    def show_idle_credits(self):
163        "Handle Idle Credits button event."
164        self.display_file_text('About - Credits', 'CREDITS.txt', 'utf-8')
165
166    def show_readme(self):
167        "Handle Readme button event."
168        self.display_file_text('About - Readme', 'README.txt', 'ascii')
169
170    def show_idle_news(self):
171        "Handle News button event."
172        self.display_file_text('About - NEWS', 'NEWS.txt', 'utf-8')
173
174    def display_printer_text(self, title, printer):
175        """Create textview for built-in constants.
176
177        Built-in constants have type _sitebuiltins._Printer.  The
178        text is extracted from the built-in and then sent to a text
179        viewer with self as the parent and title as the title of
180        the popup.
181        """
182        printer._Printer__setup()
183        text = '\n'.join(printer._Printer__lines)
184        self._current_textview = textview.view_text(
185            self, title, text, _utest=self._utest)
186
187    def display_file_text(self, title, filename, encoding=None):
188        """Create textview for filename.
189
190        The filename needs to be in the current directory.  The path
191        is sent to a text viewer with self as the parent, title as
192        the title of the popup, and the file encoding.
193        """
194        fn = os.path.join(os.path.abspath(os.path.dirname(__file__)), filename)
195        self._current_textview = textview.view_file(
196            self, title, fn, encoding, _utest=self._utest)
197
198    def ok(self, event=None):
199        "Dismiss help_about dialog."
200        self.grab_release()
201        self.destroy()
202
203
204if __name__ == '__main__':
205    from unittest import main
206    main('idlelib.idle_test.test_help_about', verbosity=2, exit=False)
207
208    from idlelib.idle_test.htest import run
209    run(AboutDialog)
210