日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/20 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 os filename='' 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()

結果展示:?

?

?

總結

以上是生活随笔為你收集整理的GUI 图形用户界面编程实例-记事本的设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。