Skip to content
This repository has been archived by the owner on Jun 29, 2021. It is now read-only.

Updated text-editor #59

Merged
merged 1 commit into from
Jun 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions exercises/text-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 9 additions & 11 deletions exercises/text-editor/texted.py
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
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():
# mimic common "save" behavior
# 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
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 8 additions & 10 deletions exercises/text-editor/textedfull.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 != '':
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)

Expand Down
18 changes: 8 additions & 10 deletions exercises/text-editor/textedfulloo.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 != '':
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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)

Expand Down
10 changes: 5 additions & 5 deletions exercises/text-editor/textedmin.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 != '':
Expand All @@ -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:
Expand All @@ -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:
Expand Down