# -*-coding:utf-8-*-
"""
1) page layout
2) Basic text editing functions, add, delete and change substitutions
3) Coding options
4) case, url encoding
5) New
6) Open
7) Save/Save As
"""
# GUI interface programming
from tkinter import * # control base package, after importing this package, all the functions under this package can be called directly
from tkinter import filedialog, messagebox
from import Combobox
from import quote
class MainWin():
# Top-level windows
def __init__(self):
top_button_width = 10
button_relief = RAISED
# Create top-level windows
root = Tk()
self.src_filename = None
= StringVar()
('0')
# (str(win_width)+'x750') # initialize window size
('Simple text notetaker')
# Width is not variable, height is variable, default is True
(width=False, height=False)
# Upper Controls
Button(root, text='Open', relief=button_relief, width=top_button_width, command=self.open_button_event) \
.grid(row=0, column=0)
Button(root, text='New', relief=button_relief, width=top_button_width, command=self.new_button_event) \
.grid(row=0, column=1)
Button(root, text='Save', relief=button_relief, width=top_button_width,
command=self.new_button_event).grid(row=0, column=2, sticky=E)
Button(root, text='Save As', relief=button_relief, width=top_button_width,
command=self.other_save_button_event).grid(row=0, column=3, sticky=W)
Button(root, text='About the Author', bg='SkyBlue', font='Arial', width=top_button_width, command=self.about_author).grid(
row=0, column=7, sticky=E)
# Separate lines
Label(root, text='').grid(row=1)
# Center Controls
Button(root, text='Find', relief=button_relief, width=top_button_width, command=self.find_button_event) \
.grid(row=2, column=0, sticky=N)
self.find_text = Text(root, width=top_button_width, height=2)
self.find_text.grid(row=2, column=1)
Label(root, text='Total number of searches').grid(row=3, column=0, sticky=N)
self.count_label = Label(root, textvariable=)
self.count_label.grid(row=3, column=1)
Button(root, text=' was replaced by ', relief=button_relief, width=top_button_width, command=self.replace_button_event).grid(
row=4, column=0, sticky=N)
self.replace_text = Text(root, width=top_button_width, height=2)
self.replace_text.grid(row=4, column=1, sticky=N)
Button(root, text='Capitalization', relief=button_relief, width=top_button_width, command=self.upper_button_event).grid(
row=5, column=0, sticky=N)
Button(root, text='Lowercase', relief=button_relief, width=top_button_width, command=self.lower_button_event).grid(
row=5, column=1, sticky=N)
Button(root, text='Code Selection', relief=button_relief, width=top_button_width,
command=self.code_change_button_event).grid(row=6, column=0, sticky=N)
self.code_chosen = Combobox(root, width=top_button_width - 2)
self.code_chosen['values'] = ('utf-8', 'gb2312', 'gbk')
self.code_chosen.grid(row=6, column=1)
Button(root, text='URL Encoding', relief=button_relief, width=top_button_width,
command=self.url_encode_button_event).grid(row=7, column=0, sticky=N)
= Text(root)
(row=2, column=2, columnspan=6, rowspan=15)
def open_button_event(self):
self.new_button_event()
# Get filename
self.src_filename = (filetypes=[('txt format', 'txt')])
if self.src_filename:
# Getting data
data = open(self.src_filename).read()
# Fill in the text control
(INSERT, data)
def new_button_event(self):
data = self.__get()
save_flag = False
if data and ('Hints', 'Do you want to save the current content?'):
if self.src_filename:
self.__sava_data(self.src_filename)
save_flag = True
else:
new_filename = (filetypes=[('txt format', 'txt')])
if new_filename:
self.__sava_data(new_filename)
save_flag = True
if save_flag:
(1.0, END)
def save_button_event(self):
filename = self.src_filename if self.src_filename else (filetypes=[('txt format', 'txt')])
if filename:
self.__sava_data(filename)
def other_save_button_event(self):
filename = (filetypes=[('txt format', 'txt')])
if filename:
self.__sava_data(filename)
def find_button_event(self):
data = self.__get()
find_data = self.find_text.get(1.0, END).strip()
if find_data not in data:
('Hints', 'The searched character does not exist')
else:
(str((find_data)))
def replace_button_event(self):
find_data = self.find_text.get(1.0, END).strip()
replace_data = self.replace_text.get(1.0, END).strip()
data = self.__get()
data = (find_data, replace_data)
self.__del_and_set(data)
def code_change_button_event(self):
try:
data = self.__get().encode(self.code_chosen.get().strip())
except:
('Hints', 'Unable to encode')
else:
self.__del_and_set(data)
def url_encode_button_event(self):
data = '\n' * 2 + '*' * 20 + 'The following is the URL encoding' + '*' * 20 + '\n' + quote(self.__get(), 'utf-8')
(END)
(END, data)
def upper_button_event(self):
data = self.__get().upper()
self.__del_and_set(data)
def lower_button_event(self):
data = self.__get().lower()
self.__del_and_set(data)
def about_author(self):
('About the Author', 'Written by MADAO Program Ape \nQQ: 1506795308')
def __sava_data(self, filename):
with open(filename, 'w') as f:
(self.__get())
('Hints', 'Saved successfully')
def __get(self):
# And note here that there must be a strip #
return (1.0, END).strip()
def __del_and_set(self, data):
(1.0, END)
(INSERT, data)
main = MainWin()
mainloop() # Run this GUI application