From 604d02ae5c841716c67d7bae794a913eca75fa50 Mon Sep 17 00:00:00 2001 From: Vidar Magnusson Date: Sat, 11 May 2019 13:25:23 +0200 Subject: [PATCH] Updated text-editor --- exercises/text-editor/README.md | 6 +++--- exercises/text-editor/texted.py | 20 +++++++++----------- exercises/text-editor/textedfull.py | 18 ++++++++---------- exercises/text-editor/textedfulloo.py | 18 ++++++++---------- exercises/text-editor/textedmin.py | 10 +++++----- 5 files changed, 33 insertions(+), 39 deletions(-) diff --git a/exercises/text-editor/README.md b/exercises/text-editor/README.md index 1043950..e922b25 100644 --- a/exercises/text-editor/README.md +++ b/exercises/text-editor/README.md @@ -7,9 +7,9 @@ Utveckla en textredigerare i Pythons inbyggda GUI-bibliotek. ## Delmoment 1. Läs igenom, kör och försök förstå texted.py. **Svårighetsgrad 1** -2. Lägg till en Text widget så att man kan skriva text. **Svårighetsgrad 1** -3. Lägg till en Scrollbar widget så att man kan scrolla. **Svårighetsgrad 2** -4. Implementera en oimplementerad funktion i taget och lägg till antingen knappar eller menyknappar så man kan köra funktionerna från programmet. **Svårighetsgrad 2** +2. Lägg till en [Text widget](https://www.tutorialspoint.com/python/tk_text.htm) så att man kan skriva text. **Svårighetsgrad 1** +3. Lägg till en [Scrollbar widget](https://www.tutorialspoint.com/python/tk_scrollbar.htm) så att man kan scrolla. **Svårighetsgrad 2** +4. Gå igenom de icke-implementerade funktionerna och implementera dem en i taget. Lägg till knappar i användargränssnittet som använder sig av dessa funktioner. **Svårighetsgrad 2** 5. Öppna källkoden i er textredigerare och redigera titeln (eller något annat) därifrån. **Svårighetsgrad 1** ## Utbyggnad diff --git a/exercises/text-editor/texted.py b/exercises/text-editor/texted.py index ec1d247..0cf4035 100644 --- a/exercises/text-editor/texted.py +++ b/exercises/text-editor/texted.py @@ -1,19 +1,18 @@ -import Tkinter as Tk -import tkFileDialog +import tkinter as Tk # Text Editor Skeleton def on_new(): # reset path and delete all text in the text box - print "Not implemented" + print("Not implemented") def on_open(): # let user choose what file to open from a dialog (tkFileDialog) # replace text in text box with text from file # handle cancelling of the dialog responsibely - print "Not implemented" + print("Not implemented") def on_save(): @@ -21,40 +20,40 @@ def on_save(): # if the path is already set, save the file using save_file(), otherwise: # let user choose a file to save the content in the text box to (tkFileDialog) # make sure the path is valid (not empty), save the file using save_file() - print "Not implemented" + print("Not implemented") def on_save_as(): # mimic common "save as" behavior # almost the same as on_save(), difference: this always opens a file dialog - print "Not implemented" + print("Not implemented") def get_all_text(): # returns all text in the text box # should be one line of code # not neccessary but may make the code in other places nicer - print "Not implemented" + print("Not implemented") def delete_all_text(): # deletes all text in the text box # should be one line of code # not neccessary but may make the code in other places nicer - print "Not implemented" + print("Not implemented") def save_file(save_path, text): # open file in save_path in write mode # write the text to the file # close the file - print "Not implemented" + print("Not implemented") def read_file(file_path): # open file in file_path # return the text - print "Not implemented" + print("Not implemented") # Initialize application @@ -84,7 +83,6 @@ def read_file(file_path): button = Tk.Button(app, text="Exit", command=quit) button.pack(side=Tk.BOTTOM, fill=Tk.X) - ###################################################### # Start the main event loop (i.e. run the tkinter program) diff --git a/exercises/text-editor/textedfull.py b/exercises/text-editor/textedfull.py index f27032e..ee9e603 100644 --- a/exercises/text-editor/textedfull.py +++ b/exercises/text-editor/textedfull.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- -import Tkinter as Tk -import tkFileDialog -import tkSimpleDialog -import tkColorChooser +import tkinter as Tk +from tkinter import filedialog, simpledialog, colorchooser # An implementation with many features without OOP @@ -61,7 +59,7 @@ def on_new(): def on_open(): global path - dialog = tkFileDialog.Open() + dialog = filedialog.Open() new_path = dialog.show() if new_path != '': @@ -76,7 +74,7 @@ def on_save(): new_path = path if new_path == '': - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') new_path = dialog.show() if new_path: @@ -87,7 +85,7 @@ def on_save(): def on_save_as(): global path - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') new_path = dialog.show() if new_path: @@ -107,7 +105,7 @@ def on_exit(): # EDIT MENU ACTIONS def on_find(): - target = tkSimpleDialog.askstring("Simple Text Editor", "Search for:") + target = simpledialog.askstring("Simple Text Editor", "Search for:") if target: index = text_input.search(target, Tk.INSERT, Tk.END) if not index: @@ -129,13 +127,13 @@ def on_select_all(): # SETTINGS MENU ACTIONS def on_background_color(): - (rgb, hex) = tkColorChooser.askcolor(settings['background_color']) + (rgb, hex) = colorchooser.askcolor(settings['background_color']) settings['background_color'] = hex text_input.config(bg=hex) def on_text_color(): - (rgb, hex) = tkColorChooser.askcolor(settings['text_color']) + (rgb, hex) = colorchooser.askcolor(settings['text_color']) settings['text_color'] = hex text_input.config(fg=hex) diff --git a/exercises/text-editor/textedfulloo.py b/exercises/text-editor/textedfulloo.py index 8ba6886..2e392d1 100644 --- a/exercises/text-editor/textedfulloo.py +++ b/exercises/text-editor/textedfulloo.py @@ -1,9 +1,7 @@ # -*- coding: utf-8 -*- -import Tkinter as Tk -import tkFileDialog -import tkSimpleDialog -import tkColorChooser +import tkinter as Tk +from tkinter import filedialog, simpledialog, colorchooser # An implementation with many features using OOP @@ -88,7 +86,7 @@ def on_new(self): self.delete_all_text() def on_open(self): - dialog = tkFileDialog.Open() + dialog = filedialog.Open() path = dialog.show() if path != '': @@ -101,7 +99,7 @@ def on_save(self): path = self.path if path == '': - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') path = dialog.show() if path: @@ -110,7 +108,7 @@ def on_save(self): self.save_file(self.path, text) def on_save_as(self): - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') path = dialog.show() if path: @@ -129,7 +127,7 @@ def on_exit(self): # EDIT MENU ACTIONS def on_find(self): - target = tkSimpleDialog.askstring("Simple Text Editor", "Search for:") + target = simpledialog.askstring("Simple Text Editor", "Search for:") if target: index = self.text_input.search(target, Tk.INSERT, Tk.END) if not index: @@ -149,12 +147,12 @@ def on_select_all(self): # SETTINGS MENU ACTIONS def on_background_color(self): - (rgb, hex) = tkColorChooser.askcolor(self.settings['background_color']) + (rgb, hex) = colorchooser.askcolor(self.settings['background_color']) self.settings['background_color'] = hex self.text_input.config(bg=hex) def on_text_color(self): - (rgb, hex) = tkColorChooser.askcolor(self.settings['text_color']) + (rgb, hex) = colorchooser.askcolor(self.settings['text_color']) self.settings['text_color'] = hex self.text_input.config(fg=hex) diff --git a/exercises/text-editor/textedmin.py b/exercises/text-editor/textedmin.py index 0de385c..6efb66b 100644 --- a/exercises/text-editor/textedmin.py +++ b/exercises/text-editor/textedmin.py @@ -1,5 +1,5 @@ -import Tkinter as Tk -import tkFileDialog +import tkinter as Tk +from tkinter import filedialog # A minimal implementation, what a solution might look like @@ -12,7 +12,7 @@ def on_new(): def on_open(): global path - dialog = tkFileDialog.Open() + dialog = filedialog.Open() new_path = dialog.show() if new_path != '': @@ -27,7 +27,7 @@ def on_save(): new_path = path if new_path == '': - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') new_path = dialog.show() if new_path: @@ -38,7 +38,7 @@ def on_save(): def on_save_as(): global path - dialog = tkFileDialog.SaveAs(defaultextension='txt') + dialog = filedialog.SaveAs(defaultextension='txt') new_path = dialog.show() if new_path: