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