600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > GUI 图形用户界面编程实例-记事本的设计

GUI 图形用户界面编程实例-记事本的设计

时间:2021-09-09 05:33:27

相关推荐

GUI 图形用户界面编程实例-记事本的设计

✨✨✨

感谢优秀的你打开了小白的文章

“希望在看文章的你今天又进步了一点点,离美好生活更近一步!”🌈

ttk 子模块控件

前面学的组件是 tkinter模块下的组件,整体风格较老。为了弥补这点不足,推出了 ttk组件。ttk组件更加美观、功能更加强大。使用 Combobox替代了原来的Listbox、 新增了 LabeledScale( 带 标 签 的 Scale) 、Notebook(多文档窗口)、Progressbar(进度条)、Treeview(数) 等组件。

使用 ttk组件与使用普通的 Tkinter组件并没有多大的区别,只要导入 ttk模块即可。

ttk 子模块的官方文档:

tkinter.ttk — Tk themed widgets — Python 3.7.12 documentation

菜单和工具栏

GUI 程序通常都有菜单,方便用户的交互。我们一般将菜单分为两种:

主菜单

主菜单通常位于 GUI 程序上方。例如:

快捷菜单

通过鼠标右键单击某个组件对象而弹出的菜单,一般是与该组件相关的操作。

主菜单

主菜单一般包含:文件、编辑、帮助等,位于 GUI 窗口的上面。创建主菜单一般有如下 4 步:

1.创建主菜单栏对象

menubar = tk.Menu(root)

2.创建菜单,并添加到主菜单栏对象

file_menu = tk.Menu(menubar) menubar.add_cascade(label=”文件”,menu=file_menu)

3.添加菜单项到 2步中的菜单

file_menu.add_command(label=” 打 开 ”) file_menu.add_command(label=” 保存”,accelerator=”^p” command=mySaveFile)file_menu.add_separator() file_menu.add_command(label=”退出”)

4.将主菜单栏添加到根窗口

root[“menu”]=menubar

记事本软件,主菜单的设计

from tkinter.filedialog import *root = Tk();root.geometry("400x400")#创建主菜单栏menubar = Menu(root)#创建子菜单menuFile = Menu(menubar)menuEdit = Menu(menubar)menuHelp = Menu(menubar)#将子菜单加入到主菜单栏menubar.add_cascade(label="文件(F)",menu=menuFile)menubar.add_cascade(label="编辑(E)",menu=menuEdit)menubar.add_cascade(label="帮助(H)",menu=menuHelp)filename = ""def openfile():global filenamew1.delete('1.0', 'end') # 先把Text控件中的内容清空with askopenfile(title="打开文件") as f:content = f.read()w1.insert(INSERT, content)filename = f.nameprint(f.name)def savefile():with open(filename, "w") as f:content = w1.get(1.0, END)f.write(content)def exit():root.quit()# 添加菜单项menuFile.add_command(label="打开", accelerator="ctrl+o", command=openfile)menuFile.add_command(label="保存", command=savefile)menuFile.add_separator() # 添加分割线menuFile.add_command(label="退出", command=exit)# 将主菜单栏加到根窗口root["menu"] = menubarw1 = Text(root, width=50, height=30)w1.pack()root.mainloop()

结果展示:

记事本完整代码实现

from tkinter import *from tkinter.filedialog import *from tkinter.messagebox import *import osfilename=''def author():showinfo('小白','简易记事本')def power():showinfo('版权信息','本公司保留版权信息,不可以把本软件用于商业目的!')def myopen():global filenamefilename=askopenfilename(defaultextension='.txt')if filename=='':filename=Noneelse:root.title('简易记事本'+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,textPadroot.title('未命名文件')filename=NonetextPad.delete(1.0,END)def save():global filenametry:f=open(filename,'w')msg=textPad.get(1.0,'end')f.write(msg)f.close()except:saveas()def saveas():f=asksaveasfile(initialfile='未命名.txt',defaultextension='.txt')global filenamefilename=ffh=open(f,'w')msg=textPad.get(1.0,END)fh.write(msg)fh.close()root.title('简易记事本'+os.path.basename(f))def cut():global textPadtextPad.event_generate('<<Cut>>')def copy():global textPadtextPad.event_generate('<<Copy>>')def paste():global textPadtextPad.event_generate('<<Paste>>')def undo():global textPadtextPad.event_generate('<<Undo>>')def redo():global textPadtextPad.event_generate('<<Redo>>')def select_all():global textPadtextPad.tag_add('sel','1.0','end')def find():global roott=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='不区分大小写',variabel=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=0if needle:pos='1.0'while True:pos=textPad.search(needle,pos,nocase=cssnstv,stopindex=END)if not pos:breaklastpos=pos+str(len(needle))textPad.tag_add('match',pos,lastpos)count+=1pos=lastpostextPad.tag_config('match',foreground='yellow',background='green')e.focus_set()t.title(str(count)+'个被匹配')def popup(event):global editmenueditmenu.tk_popup(event.x_root,event.y_root)root=Tk()root.title('简易记事本第一版')root.geometry('300x300+100+100')#geometry(wxh+xoffset+yoffset)menubar=Menu(root)#制作菜单实例,依附于父窗口root上面filemenu=Menu(menubar)#制作文件菜单项,依附于menubar菜单上面menubar.add_cascade(label='文件',menu=filemenu)#增加分层菜单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+Alt+S',command=saveas)editmenu=Menu(menubar)#制作编辑菜单项,依附于menubar菜单上面menubar.add_cascade(label='编辑',menu=editmenu)editmenu.add_command(label='撤销',accelerator='Ctrl+Z',command=undo)editmenu.add_command(label='重做',accelerator='Ctrl+Y',command=redo)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)aboutmenu=Menu(menubar)#制作关于菜单项,依附于menubar菜单上面menubar.add_cascade(label='关于',menu=aboutmenu)#增加分层菜单aboutmenu.add_command(label='作者',command=author)aboutmenu.add_command(label='版权',command=power)root.config(menu=menubar)shortcutbar=Frame(root,height=25,bg='light sea green')shortcutbar.pack(expand=NO,fill=X)Inlabel=Label(root,width=2,bg='antique white')Inlabel.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('<Control-3>',popup)root.mainloop()

结果展示:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。