Python 7 Levels, L7, Unit 82, Simple Notepad II

Powered By EmbedPress

Exercise

import tkinter as tk
import tkinter.filedialog
import tkinter.colorchooser
import tkinter.messagebox

class Application:

    def __init__(self):
        self.root = tk.Tk()
        self.root.title("Notebook")
        self.root.geometry("300x300+100+100")
        self.filename = None
        self.createWidget()

    def createWidget(self):
        #create main menu and add to window
        self.menubar = tk.Menu(self.root)
        self.root["menu"] = self.menubar

        #create file\edit\help menu and add to main menu
        self.filemenu = tk.Menu(self.menubar)
        self.menubar.add_cascade(label="File",menu=self.filemenu)
        self.editmenu = tk.Menu(self.menubar)
        self.menubar.add_cascade(label="Edit",menu=self.editmenu)
        self.helpmenu = tk.Menu(self.menubar)
        self.menubar.add_cascade(label="Help",menu=self.helpmenu)

        #add submenu to file
        self.filemenu.add_command(label="New",command=self.newfile)
        self.filemenu.add_command(label="Open",command=self.openfile)
        self.filemenu.add_command(label="Save",command=self.savefile)
        self.filemenu.add_separator()
        self.filemenu.add_command(label="Quit",command=self.exitfile)

        #add submenu to edit
        self.editmenu.add_command(label="Cut",command=self.cut)
        self.editmenu.add_command(label="Copy",command=self.copy)
        self.editmenu.add_command(label="Paste",command=self.paste)

        #add submenu to help
        self.helpmenu.add_command(label="About",command=self.show)

        #create multi lines text
        self.textpad = tk.Text(self.root,width=50,height=30)
        self.textpad.pack(fill="both")

        #add context menu
        self.contextMenu = tk.Menu(self.root)
        self.contextMenu.add_command(label="Cut",command=self.cut)
        self.contextMenu.add_command(label="Copy",command=self.copy)
        self.contextMenu.add_command(label="Paste",command=self.paste)
        self.contextMenu.add_command(label="Choose Background Color",command=self.bgcolor)
        self.root.bind("<Button-3>",self.showContextMenu)

        #add keyboard short cut
        self.root.bind("<Control-n>",lambda event:self.newfile())
        self.root.bind("<Control-o>",lambda event:self.openfile())
        self.root.bind("<Control-s>",lambda event:self.savefile())
        self.root.bind("<Control-q>",lambda event:self.exitfile())


    def show(self):
        tk.messagebox.showinfo("Software Information",\
                               "Author: Mathton\nVersion:1.01\nContact:181000000")
    def bgcolor(self):
        c = tk.colorchooser.askcolor(title="Choose Background Color",color="red")
        self.textpad.config(bg=c[1])
        
    def showContextMenu(self,event):
        self.contextMenu.post(event.x_root,event.y_root)

    def cut(self):
        self.textpad.event_generate("<<Cut>>")
    def copy(self):
        self.textpad.event_generate("<<Copy>>")
    def paste(self):
        self.textpad.event_generate("<<Paste>>")

    def newfile(self):
        name = tk.filedialog.asksaveasfilename(title="New Text File",\
                                        initialfile="Unknown.txt",\
                                        filetype=[("Text File",".txt")])
        if name == "":
            print("New Operation is cancelled")
        else: 
            self.textpad.delete(1.0,"end")
            self.filename = name
            self.root.title(self.filename)
            self.savefile()
    
    def openfile(self):
        try:
            with tk.filedialog.askopenfile(title="Open File",initialdir="d:\file",\
                                      filetype=[("Text File",".txt")]) as file:
                self.textpad.delete(1.0,"end")
                self.textpad.insert(1.0,file.read())
                self.filename = file.name
                self.root.title(self.filename)
        except AttributeError:
            print("Open Operation Cancelled")
            

    def savefile(self):
        try:
            with open(self.filename,"w") as file:
                c = self.textpad.get(1.0,"end")
                file.write(c)
        except TypeError:
            print("File Not Found")
        except FileNotFoundError:
            print("File Not Found")

    def exitfile(self):
        self.root.destroy()
            
    def run(self):
        self.root.mainloop()

app = Application()
app.run()

dahan1999

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts