Nho may ban edit code nay. Minh muon chuyen tu MenuBar sang su dung
Button thi lam sao nho may ban giup. Day la code minh da test tren
Python3. thanks.
#!/usr/bin/env python3
from tkinter import Tk, ttk, Text, Scrollbar, Menu, messagebox, filedialog, BooleanVar, Checkbutton, Label, Entry, StringVar, Grid, Frame
import os, subprocess, json, string
class Editor():
def __init__(self, root):
self.root = root
self.TITLE = "Simple Editor"
self.file_path = None
self.set_title()
frame = Frame(root)
self.yscrollbar = Scrollbar(frame, orient="vertical")
self.editor = Text(frame, yscrollcommand=self.yscrollbar.set)
self.editor.pack(side="left", fill="both", expand=1)
self.editor.config( wrap = "word", undo = True, width = 80 )
self.editor.focus()
self.yscrollbar.pack(side="right", fill="y")
self.yscrollbar.config(command=self.editor.yview)
frame.pack(fill="both", expand=1)
#create a top level menu
self.menubar = Menu(root)
#Menu item File
filemenu = Menu(self.menubar, tearoff=0)# tearoff = 0 => can't be seperated from window
filemenu.add_command(label="New", command=self.file_new)
filemenu.add_command(label="Open...", command=self.file_open)
filemenu.add_command(label="Save", command=self.file_save)
filemenu.add_command(label="Save As...", command=self.file_save_as)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.file_quit)
self.menubar.add_cascade(label="File", menu=filemenu)
# display the menu
root.config(menu=self.menubar)
def save_if_modified(self, event=None):
if self.editor.edit_modified(): #modified
response = messagebox.askyesnocancel("Save?", " Do you want to save changes?") #yes = True, no = False, cancel = None
if response: #yes/save
result = self.file_save()
if result == "saved": #saved
return True
else: #save cancelled
return None
else:
return response #None = cancel/abort, False = no/discard
else: #not modified
return True
def file_new(self, event=None):
result = self.save_if_modified()
if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
self.editor.delete(1.0, "end")
self.editor.edit_modified(False)
self.editor.edit_reset()
self.file_path = None
self.set_title()
def file_open(self, event=None, filepath=None):
result = self.save_if_modified()
if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
if filepath == None:
filepath = filedialog.askopenfilename()
if filepath != None and filepath != '':
with open(filepath) as f:
fileContents = f.read()# Get all the text from file.
# Set current text to file contents
self.editor.delete(1.0, "end")
self.editor.insert(1.0, fileContents)
self.editor.edit_modified(False)
self.file_path = filepath
def file_save(self, event=None):
if self.file_path == None:
result = self.file_save_as()
else:
result = self.file_save_as(filepath=self.file_path)
return result
def file_save_as(self, event=None, filepath=None):
if filepath == None:
filepath = filedialog.asksaveasfilename(defaultextension='.txt')
try:
with open(filepath, 'wb') as f:
text = self.editor.get(1.0, "end-1c")
f.write(bytes(text, 'UTF-8'))
self.editor.edit_modified(False)
self.file_path = filepath
self.set_title()
return "saved"
except FileNotFoundError:
print('FileNotFoundError')
return "cancelled"
def file_quit(self, event=None):
result = self.save_if_modified()
if result != None: #None => Aborted or Save cancelled, False => Discarded, True = Saved or Not modified
self.root.destroy() #sys.exit(0)
def set_title(self, event=None):
if self.file_path != None:
title = os.path.abspath(self.file_path)
else:
title = "Untitled"
self.root.title(self.TITLE + " - " + title)
if __name__ == "__main__":
root = Tk()
editor = Editor(root)
root.mainloop()