def __init__(self): self.win = Tk() # 生成win主窗口 self.win.title('Simulate Sublime Text') menu_bar = Menu(self.win) self.win.config(menu=menu_bar) file_menu = Menu(menu_bar, tearoff=0) # 去除虚线框 file_menu.add_command(label='New File', accelerator='Ctrl+N', command=self.new_file) file_menu.add_command(label='Open File...', accelerator='Ctrl+O', command=self.open_file) file_menu.add_command(label='Save', accelerator='Ctrl+S', command=self.save_file) file_menu.add_command(label='Save As...', accelerator='Ctrl+Shift+S', command=self.save_as_file) file_menu.add_separator() # 菜单选项栏增添横线 file_menu.add_command(label='Exit', command=self.exit_file) file_menu.add_command(label='Test', command=self.test) menu_bar.add_cascade(label='File', menu=file_menu) def eventhandler(event): print('按下了' + event.keysym) file_menu.bind_all('<Control-n>', lambda event: self.new_file()) file_menu.bind_all('<Control-o>', lambda event: self.open_file()) file_menu.bind_all('<Control-s>', lambda event: self.save_file()) file_menu.bind_all('<Control-S>', lambda event: self.save_file()) file_menu.bind_all('<KeyPress>', eventhandler) self.src_filename = None self.text_area = Text() self.cd_key = None self.text_area.pack() self.win.mainloop()
def __init__(self): self.win = Tk() # 生成win主窗口 self.win.title('Simulate Sublime Text') menu_bar = Menu(self.win) self.win.config(menu=menu_bar) file_menu = Menu(menu_bar, tearoff=0) # 去除虚线框 file_menu.add_command(label='New File', accelerator='Ctrl+N', command=self.new_file) file_menu.add_command(label='Open File...', accelerator='Ctrl+O', command=self.open_file) file_menu.add_command(label='Save', accelerator='Ctrl+S', command=self.save_file) file_menu.add_command(label='Save As...', accelerator='Ctrl+Shift+S', command=self.save_as_file) file_menu.add_separator() # 菜单选项栏增添横线 file_menu.add_command(label='Exit', command=self.exit_file) menu_bar.add_cascade(label='File', menu = file_menu) file_menu.bind_all('<Control-n>', lambda event:self.new_file()) file_menu.bind_all('<Control-o>', lambda event:self.open_file()) file_menu.bind_all('<Control-s>', lambda event: self.save_file()) file_menu.bind_all('<Control-S>', lambda event: self.save_file()) # 创建编辑菜单 edit_menu = Menu(menu_bar, tearoff=False) # 创建编辑下拉内容 edit_menu.add_command(label="Undo", accelerator='Ctrl+Z', command=self.undo_opr) edit_menu.add_separator() edit_menu.add_command(label="Copy", accelerator='Ctrl+C', command=self.copy_opr) edit_menu.add_command(label="Cut", accelerator='Ctrl+X', command=self.cut_opr) edit_menu.add_command(label="Paste", accelerator='Ctrl+V', command=self.paste_opr) menu_bar.add_cascade(label='Edit', menu=edit_menu) edit_menu.bind_all('<Control-z>', lambda event: self.undo_opr()) edit_menu.bind_all('<Control-c>', lambda event: self.copy_opr()) edit_menu.bind_all('<Control-x>', lambda event: self.cut_opr()) edit_menu.bind_all('<Control-v>', lambda event: self.paste_opr()) # 创建帮助菜单 help_menu = Menu(menu_bar, tearoff=0) help_menu.add_command(label='About Sublime Text', command=self.about_msg) menu_bar.add_cascade(label='Help', menu=help_menu) self.src_filename = None self.text_area = Text() self.cd_key = None self.text_area.pack() self.win.mainloop()
root = tk.Tk() root.title('') menubar = Menu(root) filemenu = Menu(menubar, tearoff=0) #menu inside application window root.config(menu=menubar) menubar.add_cascade(label="File", menu=filemenu) filemenu.add_command(label="New", command=create_file) filemenu.add_command(label="Open", command=open_file, accelerator="Ctrl+p") filemenu.add_command(label="Save", command=save_file, accelerator="Ctrl+l") filemenu.add_separator() filemenu.add_command(label="Exit", command=exit) filemenu.bind_all("<Control-p>", open_file) filemenu.bind_all("<Control-l>", save_file) filemenu.bind_all("<KeyPress>", check_for_changes) main = ttk.Frame(root) main.pack(fill="both", expand=True, padx=(20, 20), pady=(0, 20)) tk.Label(main, text="Text Editor", bg="pink").pack(fill="x") notebook = ttk.Notebook(main) notebook.pack(expand=True, fill="both") create_file() closetab = Menu(root, tearoff=0) closetab.add_command(label="Close") notebook.bind("<Button-3>", delete_tab)
# _quit与×,退出,以及退出的快捷键关联。该函数可传入任意个参数 #因为bind_all会返回一个event,所以目前只好这样改 def _quit(*args): var = msg.askyesno('', '你真的要退出吗?') if var == True: main_panel.quit() main_panel.destroy() #给File添加Save与Quit,并且给Quit添加快捷键 file_menu.add_command(label='保存', command=_save, accelerator='Ctrl+S') file_menu.add_separator() file_menu.add_command(label='退出', command=_quit, accelerator='Ctrl+E') file_menu.bind_all('<Control-e>', _quit) file_menu.bind_all('<Control-s>', _save) #将叉与_quit函数绑定 main_panel.protocol('WM_DELETE_WINDOW', _quit) #_about与 关于 绑定 def _about(): msg.showinfo('', '这是由leslie lee用Tkinter创建的GUI。\n创建时间:2019-10-05') #_help与 如何使用 绑定 def _help(): help_panel = tk.Tk() help_panel.geometry('300x200')