[Python-checkins] cpython (3.4): Issue #21933: Users can now change the font size for example code.

terry.reedy python-checkins at python.org
Wed Sep 3 22:19:01 CEST 2014


http://hg.python.org/cpython/rev/ce14092430b6
changeset:   92308:ce14092430b6
branch:      3.4
parent:      92301:27cef7476f2b
user:        Terry Jan Reedy <tjreedy at udel.edu>
date:        Wed Sep 03 16:17:41 2014 -0400
summary:
  Issue #21933: Users can now change the font size for example code.
Original patch by Lita Cho.

files:
  Lib/turtledemo/__main__.py |  93 +++++++++++++++++++------
  1 files changed, 71 insertions(+), 22 deletions(-)


diff --git a/Lib/turtledemo/__main__.py b/Lib/turtledemo/__main__.py
--- a/Lib/turtledemo/__main__.py
+++ b/Lib/turtledemo/__main__.py
@@ -17,14 +17,17 @@
   The (syntax coloured) source code appears in the left
   source code window. IT CANNOT BE EDITED, but ONLY VIEWED!
 
-  - Press START button to start the demo.
-  - Stop execution by pressing the STOP button.
-  - Clear screen by pressing the CLEAR button.
-  - Restart by pressing the START button again.
+  The demo viewer windows can be resized. The divider between text
+  and canvas can be moved by grabbing it with the mouse. The text font
+  size can be changed from the menu and with Control/Command- '-'/'+'.
+  It can also be changed on most systems with Control-mousewheel.
 
-  SPECIAL demos are those which run EVENTDRIVEN.
-  (For example clock.py - or oldTurtleDemo.py which
-  in the end expects a mouse click.):
+  Press START button to start the demo.
+  Stop execution by pressing the STOP button.
+  Clear screen by pressing the CLEAR button.
+  Restart by pressing the START button again.
+
+  SPECIAL demos, such as clock.py are those which run EVENTDRIVEN.
 
       Press START button to start the demo.
 
@@ -87,7 +90,7 @@
 from tkinter import *
 from idlelib.Percolator import Percolator
 from idlelib.ColorDelegator import ColorDelegator
-from idlelib.textView import view_text # TextViewer
+from idlelib.textView import view_text
 from importlib import reload
 from turtledemo import __doc__ as about_turtledemo
 
@@ -95,6 +98,7 @@
 import time
 
 demo_dir = os.path.dirname(os.path.abspath(__file__))
+darwin = sys.platform == 'darwin'
 
 STARTUP = 1
 READY = 2
@@ -104,7 +108,11 @@
 
 menufont = ("Arial", 12, NORMAL)
 btnfont = ("Arial", 12, 'bold')
-txtfont = ('Lucida Console', 8, 'normal')
+txtfont = ['Lucida Console', 10, 'normal']
+
+MINIMUM_FONT_SIZE = 6
+MAXIMUM_FONT_SIZE = 100
+font_sizes = [8, 9, 10, 11, 12, 14, 18, 20, 22, 24, 30]
 
 def getExampleEntries():
     return [entry[:-3] for entry in os.listdir(demo_dir) if
@@ -123,7 +131,7 @@
         root.title('Python turtle-graphics examples')
         root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
 
-        if sys.platform == 'darwin':
+        if darwin:
             import subprocess
             # Make sure we are the currently activated OS X application
             # so that our menu bar appears.
@@ -136,8 +144,7 @@
                         '-e', 'end tell',
                     ],
                     stderr=subprocess.DEVNULL,
-                    stdout=subprocess.DEVNULL,
-                )
+                    stdout=subprocess.DEVNULL,)
 
         root.grid_rowconfigure(0, weight=1)
         root.grid_columnconfigure(0, weight=1)
@@ -147,9 +154,11 @@
 
         self.mBar = Menu(root, relief=RAISED, borderwidth=2)
         self.mBar.add_cascade(menu=self.makeLoadDemoMenu(self.mBar),
-                              label='Examples', underline=0, font=menufont)
+                              label='Examples', underline=0)
+        self.mBar.add_cascade(menu=self.makeFontMenu(self.mBar),
+                              label='Fontsize', underline=0)
         self.mBar.add_cascade(menu=self.makeHelpMenu(self.mBar),
-                              label='Help', underline=0, font=menufont)
+                              label='Help', underline=0)
         root['menu'] = self.mBar
 
         pane = PanedWindow(orient=HORIZONTAL, sashwidth=5,
@@ -203,7 +212,7 @@
         hbar['command'] = text.xview
         hbar.pack(side=BOTTOM, fill=X)
 
-        text['font'] = txtfont
+        text['font'] = tuple(txtfont)
         text['yscrollcommand'] = vbar.set
         text['xscrollcommand'] = hbar.set
         text.pack(side=LEFT, fill=BOTH, expand=1)
@@ -216,7 +225,7 @@
         turtle._Screen._canvas = self._canvas = canvas = turtle.ScrolledCanvas(
                 root, 800, 600, self.canvwidth, self.canvheight)
         canvas.adjustScrolls()
-        canvas._rootwindow.bind('<Configure>', self.onResize)
+        self.makeBindings(canvas._rootwindow)
         canvas._canvas['borderwidth'] = 0
 
         self.screen = _s_ = turtle.Screen()
@@ -225,6 +234,35 @@
         turtle.RawTurtle.screens = [_s_]
         return canvas
 
+    def makeBindings(self, widget):
+        widget.bind('<Configure>', self.onResize)
+
+        shortcut = 'Command' if darwin else 'Control'
+        widget.bind_all('<%s-minus>' % shortcut, self.decrease_size)
+        widget.bind_all('<%s-underscore>' % shortcut, self.decrease_size)
+        widget.bind_all('<%s-equal>' % shortcut, self.increase_size)
+        widget.bind_all('<%s-plus>' % shortcut, self.increase_size)
+        widget.bind_all('<Control-MouseWheel>', self.update_mousewheel)
+        widget.bind('<Control-Button-4>', self.increase_size)
+        widget.bind('<Control-Button-5>', self.decrease_size)
+
+    def set_txtsize(self, size):
+        txtfont[1] = size
+        self.text['font'] = tuple(txtfont)
+        self.output_lbl['text'] = 'Font size %d' % size
+
+    def decrease_size(self, dummy=None):
+        self.set_txtsize(max(txtfont[1] - 1, MINIMUM_FONT_SIZE))
+
+    def increase_size(self, dummy=None):
+        self.set_txtsize(min(txtfont[1] + 1, MAXIMUM_FONT_SIZE))
+
+    def update_mousewheel(self, event):
+        # For wheel up, event.delte = 120 on Windows, -1 on darwin.
+        # X-11 sends Control-Button-4 event instead.
+        (self.decrease_size() if (event.delta < 0 and not darwin)
+            else self.increase_size())
+
     def configGUI(self, start, stop, clear, txt="", color="blue"):
         self.start_btn.config(state=start,
                               bg="#d00" if start == NORMAL else "#fca")
@@ -238,13 +276,25 @@
         menu = Menu(master)
 
         for entry in getExampleEntries():
-            def loadexample(x):
-                def emit():
-                    self.loadfile(x)
-                return emit
+            def load(entry=entry):
+                self.loadfile(entry)
             menu.add_command(label=entry, underline=0,
-                             font=menufont, command=loadexample(entry))
+                             font=menufont, command=load)
+        return menu
 
+    def makeFontMenu(self, master):
+        menu = Menu(master)
+        menu.add_command(label="Decrease (C-'-')", command=self.decrease_size,
+                         font=menufont)
+        menu.add_command(label="Increase (C-'+')", command=self.increase_size,
+                         font=menufont)
+        menu.add_separator()
+
+        for size in font_sizes:
+            def resize(size=size):
+                self.set_txtsize(size)
+            menu.add_command(label=str(size), underline=0,
+                             font=menufont, command=resize)
         return menu
 
     def makeHelpMenu(self, master):
@@ -254,7 +304,6 @@
             def show(help_label=help_label, help_file=help_file):
                 view_text(self.root, help_label, help_file)
             menu.add_command(label=help_label, font=menufont, command=show)
-
         return menu
 
     def refreshCanvas(self):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list