forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepad-multitab.py
More file actions
128 lines (90 loc) · 3.37 KB
/
Copy pathnotepad-multitab.py
File metadata and controls
128 lines (90 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
#
# modifications for: http://codeshot.in/pythongui/notepad.php
#
# FB: https://www.facebook.com/groups/learnpython.org/permalink/1180950268636255/
#
import os # os.path.basename
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
# --- constants ---
# empty
# --- classes ---
class Document(tk.Text):
def __init__(self, master, filename=None):
super().__init__(master) # Python 3 only
self.filename = filename
# on Linux background is not white
self.config(bg='white')
def debug(self):
print(self.master, self.master.tab(0))
def save(self):
if not self.filename:
class Notepad(tk.Tk):
def __init__(self):
super().__init__() # Python 3 only
self.create_GUI()
self.docs = [] # keep documents
doc = Document(self.tabs)
self.docs.append(doc)
self.tabs.add(doc, text='noname')
def create_GUI(self):
self.geometry('800x600')
self.title("TkNotes")
self.tabs = ttk.Notebook(self)
self.tabs.pack(fill=tk.BOTH, expand=True)
# menus
menu = tk.Menu(self)
self.config(menu=menu)
# file menu
file_menu = tk.Menu(menu)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Open File", command=self.open_command)
file_menu.add_command(label="Save As", command=self.save_command)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=self.exit_command)
# edit menu
edit_menu = tk.Menu(menu)
menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Undo", command=self.dummy)
edit_menu.add_command(label="Redo", command=self.dummy)
edit_menu.add_command(label="Cut", command=self.dummy)
edit_menu.add_command(label="Copy", command=self.dummy)
edit_menu.add_command(label="Paste", command=self.dummy)
# help menu
help_menu = tk.Menu(menu)
menu.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=self.about_command)
def run(self):
# start the engine :)
self.mainloop()
def dummy(self):
messagebox.showinfo("UPS!", "Function not implemented.")
def open_command(self):
file_ = filedialog.askopenfile(mode="r")
if file_:
# get file name
name = os.path.basename(file_.name)
doc = Document(self.tabs)
content = file_.read()
doc.insert("1.0", content)
file_.close()
self.docs.append(doc)
self.tabs.add(doc, text=name)
def save_command(self):
file_ = filedialog.asksaveasfile(mode="w")
if file_:
content = self.text.get("1.0", tk.END) # tk.END+"-1c" # do we need "-1c" ???
file_.write(content)
file_.close()
def exit_command(self):
if messagebox.askokcancel("Quit", "Do you really want to quit?"):
self.destroy()
def about_command(self):
messagebox.showinfo("About", "TkNotes 0.1\nBartłomiej \"furas\" Burek\nCopyright 2016")
# --- functions ---
# empty
# --- main ---
Notepad().run() # or Notepad().mainloop()