Python?GUI?圖形用戶(hù)界面
GUI介紹
圖形用戶(hù)界面(Graphical User Interface,簡(jiǎn)稱(chēng) GUI,又稱(chēng)圖形用戶(hù)接口)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶(hù)界面。圖形用戶(hù)界面是一種人與計(jì)算機(jī)通信的界面顯示格式,允許用戶(hù)使用鼠標(biāo)等輸入設(shè)備操縱屏幕上的圖標(biāo)或菜單選項(xiàng),以選擇命令、調(diào)用文件、啟動(dòng)程序或執(zhí)行其它一些日常任務(wù)。與通過(guò)鍵盤(pán)輸入文本或字符命令來(lái)完成例行任務(wù)的字符界面相比,圖形用戶(hù)界面有許多優(yōu)點(diǎn)。圖形用戶(hù)界面由窗口、下拉菜單、對(duì)話框及其相應(yīng)的控制機(jī)制構(gòu)成,在各種新式應(yīng)用程序中都是標(biāo)準(zhǔn)化的,即相同的操作總是以同樣的方式來(lái)完成,在圖形用戶(hù)界面,用戶(hù)看到和操作的都是圖形對(duì)象,應(yīng)用的是計(jì)算機(jī)圖形學(xué)的技術(shù)。
在設(shè)計(jì)GUI程序的過(guò)程中,需要對(duì)用戶(hù)界面進(jìn)行渲染,達(dá)到色彩與便捷智能化一體。而在Python內(nèi)置庫(kù)里面,有一個(gè)自帶的就是tkinter庫(kù),我們直接導(dǎo)入 使用即可。
簡(jiǎn)單操作
import tkinter top=tkinter.Tk()#生成一個(gè)主窗口 # 這里面可以作為消息循環(huán),添加窗口功能 label=tkinter.Label(top,text="圖形界面程序!") label.pack()#將標(biāo)簽label添加到窗口中 button1=tkinter.Button(top,text="按鈕1") button1.pack(side=tkinter.LEFT)#將按鈕1添加到窗口里 button2=tkinter.Button(top,text="按鈕2") button2.pack(side=tkinter.RIGHT)#將按鈕2添加到窗口里 top.mainloop()#進(jìn)入消息循環(huán)
tkinter組件介紹
import tkinter import tkMessageBox top = tkinter.Tk() def helloCallBack(): tkMessageBox.showinfo("Hello Python", "Hello Runoob") B = tkinter.Button(top, text="點(diǎn)我", command=helloCallBack) B.pack() top.mainloop()
向窗體中添加按鈕控件
import tkinter root=tkinter.Tk()#生成一個(gè)主窗口對(duì)象 button1=tkinter.Button(root,anchor=tkinter.E,#設(shè)置文本對(duì)齊方式 text="按鈕1",width=30,#設(shè)置按鈕寬度 height=7) button1.pack()#將按鈕添加到主窗口 button2=tkinter.Button(root,text="按鈕2",bg="red")#設(shè)置背景按鈕色 button2.pack() button3=tkinter.Button(root,text="按鈕3",width=12,height=1) button3.pack() button4=tkinter.Button(root,text="按鈕4",width=40,height=7, state=tkinter.DISABLED)#設(shè)置按鈕為禁用 button4.pack() root.mainloop()
使用文本框控件
在tkinter庫(kù)中可以實(shí)現(xiàn)信息接收和用戶(hù)的信息輸入工作,在Python程序中,使用tkinter.Entry和tkinter.text可以創(chuàng)建單行文本和多行文本框組件,通過(guò)傳遞一些屬性來(lái)解決顏色問(wèn)題。
import tkinter root=tkinter.Tk() entry1=tkinter.Entry(root, show="*"#設(shè)置顯示文本是星號(hào) ) entry1.pack() entry2=tkinter.Entry(root,show="$",width=50) entry2.pack() entry3=tkinter.Entry(root,bg="red",fg="blue")#設(shè)置文本框的前景色 entry3.pack() entry4=tkinter.Entry(root,state=tkinter.DISABLED) entry4.pack() entry5=tkinter.Entry(root,selectbackground="red",selectforeground="gray")#分別設(shè)置文本背景色和文本前景色 entry5.pack() edit1=tkinter.Text(root,selectbackground="red",selectforeground="gray") edit1.pack() root.mainloop()
使用菜單控件
在使用菜單控件的時(shí)候,和我們使用其他控件有所不同,我們需要使用創(chuàng)建主窗口的方法config()將菜單添加到窗口中。
import tkinter root=tkinter.Tk() menu=tkinter.Menu(root) # 添加主菜單選項(xiàng) submenu=tkinter.Menu(menu,tearoff=0) submenu.add_command(label="打開(kāi)") submenu.add_command(label="保存") submenu.add_command(label="關(guān)閉") menu.add_cascade(label="文件",menu=submenu)#設(shè)置標(biāo)頭簽名稱(chēng) submenu=tkinter.Menu(menu,tearoff=0) submenu.add_command(label="復(fù)制") submenu.add_command(label="粘貼") submenu.add_separator() submenu.add_command(label="剪切") menu.add_cascade(label="編輯",menu=submenu) submenu=tkinter.Menu(menu,tearoff=0) submenu.add_command(label="黑客模式") submenu.add_command(label="植入病毒") submenu.add_command(label="獲取密碼") menu.add_cascade(label="幫助",menu=submenu) root.config(menu=menu)#將菜單添加到主窗口 root.mainloop()
自己可定義不同的選項(xiàng),之后我們?cè)谶x項(xiàng)里面嵌入不同的功能,這樣就達(dá)到了一個(gè)簡(jiǎn)單圖形界面軟件的開(kāi)發(fā)。
使用標(biāo)簽控件
import tkinter root=tkinter.Tk() label1=tkinter.Label(root, # anchor=tkinter.E,#設(shè)置標(biāo)簽文本位置 bg="yellow",#設(shè)置標(biāo)簽的背景色 fg="blue",#設(shè)置標(biāo)簽的前景色 text="我是王小王\n!",#設(shè)置標(biāo)簽顯示的文本 justify=tkinter.CENTER, width=40,#設(shè)置標(biāo)簽寬度 height=5#設(shè)置標(biāo)簽高度 ) label1.pack()#將標(biāo)簽1添加到主窗口 label2=tkinter.Label(root, text="你好\nPython!",#設(shè)置標(biāo)簽顯示的文本 justify=tkinter.LEFT, width=40,#設(shè)置標(biāo)簽寬度 height=5#設(shè)置標(biāo)簽高度 ) label2.pack() label3=tkinter.Label(root, text="你好\nPython!",#設(shè)置標(biāo)簽顯示的文本 justify=tkinter.RIGHT, width=40,#設(shè)置標(biāo)簽寬度 height=5#設(shè)置標(biāo)簽高度 ) label3.pack() label4=tkinter.Label(root, text="你好\nPython!",#設(shè)置標(biāo)簽顯示的文本 justify=tkinter.CENTER, width=40,#設(shè)置標(biāo)簽寬度 height=5#設(shè)置標(biāo)簽高度 ) label4.pack() root.mainloop()
使用單選按鈕和復(fù)選按鈕組件
import tkinter root=tkinter.Tk() r=tkinter.StringVar()#生成字符串變量 r.set("1") radio=tkinter.Radiobutton(root, variable=r, value="1",#設(shè)置單選按鈕時(shí)的變量值 text="單選按鈕1", ) radio.pack() radio=tkinter.Radiobutton(root, variable=r, value="2",#設(shè)置單選按鈕時(shí)的變量值 text="單選按鈕2", ) radio.pack() radio=tkinter.Radiobutton(root, variable=r, value="3",#設(shè)置單選按鈕時(shí)的變量值 text="單選按鈕3", ) radio.pack() radio=tkinter.Radiobutton(root, variable=r, value="4",#設(shè)置單選按鈕時(shí)的變量值 text="單選按鈕4", ) radio.pack() c=tkinter.IntVar()#生成整型變量 c.set(1) check=tkinter.Checkbutton(root,text="復(fù)選按鈕", variable=c,#復(fù)選按鈕關(guān)聯(lián)的變量 onvalue=1,#設(shè)置復(fù)選按鈕時(shí)的變量值1 offvalue=2)#設(shè)置復(fù)選按鈕時(shí)的變量值2 check.pack() root.mainloop() print(r.get()) print(c.get())
使用繪圖組件
import tkinter root=tkinter.Tk() canvas=tkinter.Canvas(root, width=600, height=480, bg="white")#設(shè)置繪圖控件的背景色 '''' ............... '''
到此這篇關(guān)于Python GUI 圖形用戶(hù)界面的文章就介紹到這了,更多相關(guān)Python GUI內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python matplotlib繪制散點(diǎn)圖的實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python matplotlib繪制散點(diǎn)圖的相關(guān)資料,所謂散點(diǎn)圖就是反映兩組變量每個(gè)數(shù)據(jù)點(diǎn)的值,并且從散點(diǎn)圖可以看出它們之間的相關(guān)性,需要的朋友可以參考下2021-06-06Flask中jinja2的繼承實(shí)現(xiàn)方法及實(shí)例
在本篇文章里小編給大家分享的是一篇關(guān)于Flask中jinja2的繼承實(shí)現(xiàn)方法及實(shí)例,有興趣的朋友們可以學(xué)習(xí)下。2021-03-03react中useLayoutEffect 和useEffect區(qū)別
本文主要介紹了react中useLayoutEffect 和useEffect區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12淺談Python從全局與局部變量到裝飾器的相關(guān)知識(shí)
今天給大家?guī)?lái)的是關(guān)于Python的相關(guān)知識(shí),文章圍繞著Python從全局與局部變量到裝飾器的相關(guān)知識(shí)展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python利用3D引擎寫(xiě)一個(gè)Pong游戲
之前,我們嘗試過(guò)用pygame做了一個(gè)2D的Pong游戲。本文將利用強(qiáng)大的3D引擎Ursina制作一個(gè)3D版的Pong游戲。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2023-01-01已安裝Pytorch卻提示no?moudle?named?'torch'(沒(méi)有名稱(chēng)為torch
這篇文章主要給大家介紹了關(guān)于已安裝Pytorch卻提示no?moudle?named?'torch'(沒(méi)有名稱(chēng)為torch的模塊)的相關(guān)資料,當(dāng)提示"No module named 'torch'"時(shí),可能是由于安裝的Pytorch版本與當(dāng)前環(huán)境不匹配導(dǎo)致的,需要的朋友可以參考下2023-11-11Python爬蟲(chóng)入門(mén)教程01之爬取豆瓣Top電影
這篇文章主要介紹了Python爬蟲(chóng)入門(mén)教程01:豆瓣Top電影爬取的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01