Python GUI之如何使用tkinter控件_第1頁
Python GUI之如何使用tkinter控件_第2頁
Python GUI之如何使用tkinter控件_第3頁
Python GUI之如何使用tkinter控件_第4頁
Python GUI之如何使用tkinter控件_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

第PythonGUI之如何使用tkinter控件目錄一、功能按鈕二、文本框三、練習一四、練習二:計算器

一、功能按鈕

格式:Button(父對象,options,…)

父對象:表示當前按鈕建立在哪一個窗口下。

options:下面總結一部常用的。

1.bg或者background:背景色

2.fg或者foreground:前景色

3.command:單擊按鈕時,執(zhí)行此方案

4.font:字體

5.height:字符高度

6.width:字符寬度

7.image:按鈕上的圖片

8.padx:設置文字與按鈕左右間隔

9.pady:設置文字與按鈕上下間隔

10.state:NORMAL表示啟用按鈕,DISABLED表示禁用按鈕

11.text:字符

Button初體驗:這里我們建造一個制造女朋友的工廠,你只需要點一下按鈕就可以告別單身狗

fromtkinterimport*

defmsgShow():

label["text"]="我是你的女朋友,恭喜你告別單身"

label["bg"]="lightyellow"

label["fg"]="blue"

#實例對象

root=Tk()

root.title("女朋友工廠")

root.geometry("300x200+500+500")

label=Label(root)

btn=Button(root,text="開始制造",command=msgShow,width=15)

btnCls=Button(root,text="結束",command=root.destroy,width=15)

#控件顯示

label.pack(side=TOP)

btn.pack(side=LEFT,padx=20)

btnCls.pack(side=RIGHT,padx=20)

#窗體暫停

root.mainloop()

Button進階體驗:上面的工廠只能造一個女朋友,但是作為一個海王,你現(xiàn)在想多擁有幾個女朋友,那么現(xiàn)在你需要一個能制造多個女朋友的工廠【使用Lambda表達式】

fromtkinterimport*

defmkGrilFriend(name):

lbl.config(text=str("我是"+name+"小姐姐,從現(xiàn)在開始我就是你的女朋友啦?。。?))

root=Tk()

root.title("改變窗體顏色")

root.geometry("500x100")

lbl=Label(root)

lbl.pack()

exitBtn=Button(root,text="退出",command=root.destroy)

#制造女友

oneBtn=Button(root,text="1號女友",command=lambda:mkGrilFriend("田園my老師"))

twoBtn=Button(root,text="2號女友",command=lambda:mkGrilFriend("三上yy老師"))

exitBtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)

twoBtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)

oneBtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)

root.mainloop()

運行結果:

二、文本框

格式:Entry(父對象,options,…)

options參數(shù)主要包括以下:

1.bg:背景色

2.fg:字體顏色

3.command:當用戶更改內(nèi)容時,觸發(fā)此函數(shù)

4.font:字體

5.height:字符高度

6.width:字符寬度

7.selectbackground:被選定字符的背景色

8.show:常用于隱藏顯示密碼字段,ps:show='*'

9.state:NORMAL正常輸入,DISABLE表示禁止輸入

10.xscrollcommand:在x軸顯示滾動條

包括方法:

1.get()獲取文本框內(nèi)的字符串內(nèi)容:ety.get()

2.insert()插入字符串到文本框:ety.insert(開始位置索引,要插入的字符串)

3.delete()刪除文本框中的字符串:ety.delete(開始位置索引,截止位置索引:END等于全刪)

4.eval()數(shù)學表達公式:results=eval(2+3*5)

三、練習一

fromtkinterimport*

root=Tk()

root.title("輸入表格")

root.geometry("300x200")

nameLbl=Label(root,text="Name")

nameLbl.grid(row=0,column=0)

addressLbl=Label(root,text="Address")

addressLbl.grid(row=1,column=0)

nameEty=Entry(root)

addressEty=Entry(root)

nameEty.grid(row=0,column=1)

addressEty.grid(row=1,column=1)

root.mainloop()

運行:

四、練習二:計算器

fromtkinterimport*

#計算函數(shù)

defcalculate():

result=eval(equ.get())#獲取輸入公式

equ.set(equ.get()+"=\n"+str(result))#輸入公式+回車換行+結果

#顯示到Label

defshow(buttonString):

content=equ.get()#獲取公式變量,并拼接到content后面

ifcontent=="0":

content=""

equ.set(content+buttonString)#顯示到label

defbackspace():

equ.set(str(equ.get()[:-1]))#equ變量-1

defclear():

equ.set("0")

root=Tk()

root.title("計算器")

#公共變量,記錄公式

equ=StringVar()

equ.set("0")

#textvariable:指定一個變量刷新text值,這里的equ的set屬性改變,label的text也會變化

label=Label(root,width=50,height=2,relief="raised",anchor=SE,textvariable=equ)

#columnspan:橫跨4個按鈕

label.grid(row=0,column=0,columnspan=4,padx=5,pady=5)

#第二行[0,1,2,3列]

clearBtn=Button(root,text="C",fg="blue",width=10,command=clear).grid(row=1,column=0,pady=5)

Button(root,text="DEL",width=10,command=backspace).grid(row=1,column=1)

Button(root,text="%",width=10,command=lambda:show("%")).grid(row=1,column=2)

Button(root,text="/",width=10,command=lambda:show("/")).grid(row=1,column=3)

#第三行[0,1,2,3列]

Button(root,text="7",width=10,command=lambda:show("7")).grid(row=2,column=0,pady=5)

Button(root,text="8",width=10,command=lambda:show("8")).grid(row=2,column=1)

Button(root,text="9",width=10,command=lambda:show("9")).grid(row=2,column=2)

Button(root,text="*",width=10,command=lambda:show("*")).grid(row=2,column=3)

#第四行[0,1,2,3列]

Button(root,text="4",width=10,command=lambda:show("4")).grid(row=3,column=0,pady=5)

Button(root,text="5",width=10,command=lambda:show("5")).grid(row=3,column=1)

Button(root,text="6",width=10,command=lambda:show("6")).grid(row=3,column=2)

Button(root,text="-",width=10,command=lambda:show("-")).grid(row=3,column=3)

#第五行[0,1,2,3列]

Button(root,text="1",width=10,command=lambda:show("1")).grid(row=4,column=0,pady=5)

Button(root,text="2",width=10,command=lambda:show("2")).grid(row=4,column=1)

Button(root,text="3",width=10,command=lambda:show("3")).grid(row=4,column=2)

Button(root,text="+",width=10,command=lambda:show("+")).grid(row=4,column=3)

#第六行[0,1,2,3列]

Button(root,text="0",width=24,command=lambda:sho

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論