Issue #21933: Users can now change the font size for example code.
Original patch by Lita Cho.
This commit is contained in:
parent
282124b8c4
commit
c6a1c02cce
|
@ -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 @@ import os
|
|||
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 turtle
|
|||
import time
|
||||
|
||||
demo_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
darwin = sys.platform == 'darwin'
|
||||
|
||||
STARTUP = 1
|
||||
READY = 2
|
||||
|
@ -104,7 +108,11 @@ EVENTDRIVEN = 5
|
|||
|
||||
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 @@ class DemoWindow(object):
|
|||
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 @@ class DemoWindow(object):
|
|||
'-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 @@ class DemoWindow(object):
|
|||
|
||||
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 @@ class DemoWindow(object):
|
|||
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 @@ class DemoWindow(object):
|
|||
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 @@ class DemoWindow(object):
|
|||
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 @@ class DemoWindow(object):
|
|||
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 @@ class DemoWindow(object):
|
|||
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):
|
||||
|
|
Loading…
Reference in New Issue