help_about.py 8.7 KB

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