tkinter实例(简易记事本)

# encoding=utf-8
from tkinter import *
from tkinter.filedialog import *
from tkinter.messagebox import *
import os
'''打开文件的功能目前不是很完善'''

filename = ''
def author():
	showinfo('helo', 'linlin')

def power():
	showinfo('版权信息', '版权信息不保留,随便拷贝')

def myopen():
	global filename
	filename = askopenfilename(defaultextension = '.txt')
	if filename == '':
		filename = None
	else:
		root.title('linlin-note' + os.path.basename(filename))
		textPad.delete(1.0, END)
		f = open(filename, 'r')
		textPad.insert(1.0, f.read())
		f.close()

def new():
	global root, filename, textPad
	root.title('未命名文件')
	filename = None
	textPad.delete(1.0, END)

def save():
	global filename
	try:
		f = open(filename, 'w')
		msg = textPad.get(1.0, 'end')
		f.write(msg)
		f.close()
	except:
		saveas()

def saveas():
	f = asksaveasfilename(initialfile='未命名.txt', defaultextension='.txt')
	global filename
	filename = f
	fh = open(f, 'w')
	msg = textPad.get(1.0, END)
	fh.write(msg)
	fh.close
	root.title('linlin 记事本'+os.path.basename(f))

def cut():
	global textPad
	textPad.event_generate('<<Cht>>')

def copy():
	global textPad
	textPad.event_generate('<<Copy>>')

def paste():
	global textPad
	textPad.event_generate('<<Paste>>')

def undo():
	global textPad
	textPad.event_generate('<<Undo>>')

def redo():
	global textPad
	textPad.event_generate('<<Redo>>')

def select_all():
	global textPad
	textPad.event_generate('sel', '1.0', 'end')

def find():
	global root
	t = Toplevel(root)
	t.title('查找')
	# 设置窗口大小
	t.geometry('260x60+200+250')
	t.transient(root)
	Label(t,text='查找:').grid(row=0, column=0, sticky='e')
	v = StringVar()
	e = Entry(t, width=20, textvariable=v)
	e.grid(row = 0, column=1, padx=2, pady=2, sticky='we')
	e.focus_set()
	c = IntVar()
	Checkbutton(t, text='不区分大小写', variable=c).grid(row=1, column=1, sticky='e')
	Button(t, text='查找所有', command=lambda:search(v.get(), c.get(), textPad, t, e)).grid(row=0, column=2,sticky='e' + 'w', padx=2, pady=2)

	def close_search():
		textPad.tag_remove('match', '1.0', END)
		t.destroy()
	t.protocol('WM_DELETE_WINDOW', close_search)

def search(needle, cssnstv, textPad, t, e):
	textPad.tag_remove('match', '1.0', END)
	count = 0
	if needle:
		pos = '1.0'
		while True:
			pos = textPad.search(needle, pos, nocase = cssnstv, stopindex=END)
			if not pos: break
			lastpos = pos + str(len(needle))
			textPad.tag_add('match', pos, lastpos)
			count += 1
			pos = lastpos
		textPad.tag_config('match', foreground = 'yellow', background='green')
		e.focus_set()
		t.title(str(count) + '个被匹配')
def popup(event):
	global editmenu
	editmenu.tk_popup(event.x_root, event.y_root)


root = Tk()
root.title('林林记事本第一版')
root.geometry('300x300+100+100')
menubar = Menu(root)

filemenu = Menu(menubar)
filemenu.add_command(label='新建', accelerator='Ctrl+N', command=new)
filemenu.add_command(label='打开', accelerator='Ctrl+O', command=myopen)
filemenu.add_command(label='保存', accelerator='Ctrl+S', command=save)
filemenu.add_command(label='另存为', accelerator='Ctrl+Shift+S', command=saveas)
menubar.add_cascade(label='文件', menu=filemenu)

editmenu = Menu(menubar)
editmenu.add_command(label='撤销', accelerator='Ctrl+Z', command=undo)
editmenu.add_command(label='重做', accelerator='Ctrl+Y', command=redo)
editmenu.add_separator()
editmenu.add_command(label='剪切', accelerator='Ctrl+X', command=cut)
editmenu.add_command(label='复制', accelerator='Ctrl+C', command=copy)
editmenu.add_command(label='粘贴', accelerator='Ctrl+V', command=paste)
editmenu.add_separator()
editmenu.add_command(label='查找', accelerator='Ctrl+F', command=find)
editmenu.add_command(label='全选', accelerator='Ctrl+A', command=select_all)
menubar.add_cascade(label='编辑', menu=editmenu)

aboutmenu = Menu(menubar)
aboutmenu.add_command(label='作者', command=author)
aboutmenu.add_command(label='版权', command=power)
menubar.add_cascade(label='关于', menu=aboutmenu)

root.config(menu = menubar)
# root['menu'] = menubar

shortcutbar = Frame(root, height=25, bg='light sea green')
shortcutbar.pack(expand=NO, fill=X)
lnlabel = Label(root, width=2, bg='antique white')
lnlabel.pack(side=LEFT, anchor='nw', fill=Y)

textPad = Text(root, undo=True)
textPad.pack(expand=YES, fill=BOTH)
scroll = Scrollbar(textPad)
textPad.config(yscrollcommand=scroll.set)
scroll.config(command = textPad.yview)
scroll.pack(side=RIGHT,fill=Y)

textPad.bind('<Control-N>',new)
textPad.bind('<Control-n>',new)
textPad.bind('<Control-O>',myopen)
textPad.bind('<Control-o>',myopen)
textPad.bind('<Control-S>',save)
textPad.bind('<Control-s>',save)
textPad.bind('<Control-A>',select_all)
textPad.bind('<Control-a>',select_all)
textPad.bind('<Control-F>',find)
textPad.bind('<Control-f>',find)

textPad.bind('<Button-3>',popup)

root.mainloop()

 

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个基于tkinter库的简单记事本设计: ```python import tkinter as tk from tkinter import filedialog, messagebox class Notepad: current_file = None def __init__(self, master): self.master = master master.title("Untitled - Notepad") master.geometry("500x500") self.text_area = tk.Text(master, font=("Helvetica", 12)) self.text_area.pack(expand=True, fill="both") # create a menu bar self.menu_bar = tk.Menu(master) self.file_menu = tk.Menu(self.menu_bar, tearoff=0) self.edit_menu = tk.Menu(self.menu_bar, tearoff=0) # add file menu options self.file_menu.add_command(label="New", command=self.new_file) self.file_menu.add_command(label="Open", command=self.open_file) self.file_menu.add_command(label="Save", command=self.save_file) self.file_menu.add_command(label="Save As", command=self.save_file_as) self.menu_bar.add_cascade(label="File", menu=self.file_menu) # add edit menu options self.edit_menu.add_command(label="Cut", command=self.cut) self.edit_menu.add_command(label="Copy", command=self.copy) self.edit_menu.add_command(label="Paste", command=self.paste) self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu) # add menu bar to the main window master.config(menu=self.menu_bar) def new_file(self): self.current_file = None self.master.title("Untitled - Notepad") self.text_area.delete(1.0, tk.END) def open_file(self): file_path = filedialog.askopenfilename() if file_path: self.current_file = file_path self.master.title(self.current_file + " - Notepad") self.text_area.delete(1.0, tk.END) with open(file_path, "r") as file: text = file.read() self.text_area.insert(tk.END, text) def save_file(self): if self.current_file: with open(self.current_file, "w") as file: text = self.text_area.get(1.0, tk.END) file.write(text) else: self.save_file_as() def save_file_as(self): file_path = filedialog.asksaveasfilename(defaultextension=".txt") if file_path: self.current_file = file_path self.master.title(self.current_file + " - Notepad") with open(file_path, "w") as file: text = self.text_area.get(1.0, tk.END) file.write(text) def cut(self): self.text_area.event_generate("<<Cut>>") def copy(self): self.text_area.event_generate("<<Copy>>") def paste(self): self.text_area.event_generate("<<Paste>>") root = tk.Tk() notepad = Notepad(root) root.mainloop() ``` 这个记事本支持以下功能: - 创建新文件 - 打开文件 - 保存文件 - 另存为 - 剪切 - 复制 - 粘贴 你可以通过运行上面的代码来体验这个记事本的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值