From f6a09ec35a04cbfb7de3d13aad2841e4e3e203f6 Mon Sep 17 00:00:00 2001 From: Peter Bittner Date: Sat, 30 Mar 2019 13:19:48 +0100 Subject: [PATCH] Alternative behavior for exit, help, license builtins --- pythonturtle/application.py | 6 +++-- pythonturtle/turtleprocess.py | 48 ++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/pythonturtle/application.py b/pythonturtle/application.py index 74f4066..b8a386b 100644 --- a/pythonturtle/application.py +++ b/pythonturtle/application.py @@ -42,6 +42,7 @@ def __init__(self, *args, **keywords): self.init_help_screen() self.turtle_process = turtleprocess.TurtleProcess() + self.turtle_process.window = self self.turtle_process.start() self.turtle_queue = self.turtle_process.turtle_queue @@ -220,6 +221,7 @@ def on_about(self, event=None): def run(): multiprocessing.freeze_support() app = wx.App() - ApplicationWindow(None, -1, pythonturtle.name, size=(600, 600)) - # import cProfile; cProfile.run("app.MainLoop()") + ApplicationWindow(parent=None, + title=pythonturtle.name, + size=(600, 600)) app.MainLoop() diff --git a/pythonturtle/turtleprocess.py b/pythonturtle/turtleprocess.py index 91a8f70..40757ed 100644 --- a/pythonturtle/turtleprocess.py +++ b/pythonturtle/turtleprocess.py @@ -2,12 +2,13 @@ A TurtleProcess is a subclass of ``multiprocessing.Process``. It is the process from which the user of PythonTurtle works. """ +import builtins import copy import math import multiprocessing import sys import time -import builtins +import webbrowser from . import shelltoprocess from .misc import smartsleep @@ -51,11 +52,50 @@ def send_report(self): def run(self): - builtins.help = builtins.license = builtins.exit = \ - lambda *args, **kwargs: print('Not supported') - self.turtle = Turtle() + def exit(): + """ + Close the app when a user types `exit()` or `quit()`. + + The built-in exit and quit functions terminate the + interpreter hence making the application unusable. We + terminate the application directly instead. + """ + print(self.window.Close()) + print(self.window.Destroy()) + + builtins.exit = exit + builtins.quit = exit + + def help(object=None): + """ + Show a command's docstring or the app's help screen. + + The built-in help function prints to the console and is + interactive there, thus blocking the UI. We simply display + the docstring of an object, when called with an argument, + or show the application help screen otherwise. + """ + if object: + print(object.__doc__) + else: + self.window.show_help() + + builtins.help = help + + def license(): + """ + Open a browser window with Python's license explained. + + The built-in license function is interactive and blocks the + UI. We open the default web browser with the Python website + displaying the license instead. + """ + webbrowser.open('https://docs.python.org/3/license.html') + + builtins.license = license + def go(distance): """ Makes the turtle walk the specified distance. Use a negative number