Python語言編程基礎(chǔ)圖形界面編程課件_第1頁
Python語言編程基礎(chǔ)圖形界面編程課件_第2頁
Python語言編程基礎(chǔ)圖形界面編程課件_第3頁
Python語言編程基礎(chǔ)圖形界面編程課件_第4頁
Python語言編程基礎(chǔ)圖形界面編程課件_第5頁
已閱讀5頁,還剩161頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、Python程序設(shè)計(jì)教程授課教師:第1頁,共166頁。第8章 圖形界面編程課程描述前面介紹的Python程序都只能輸出字符串和數(shù)值,這顯然不能滿足實(shí)際應(yīng)用的需求。Python提供了一些用于圖形界面編程的模塊,包括Tkinter模塊、wxWidgets模塊、easygui模塊和wxpython模塊。由于篇幅所限,本章以Tkinter為例介紹通過一些使用的Python工具包進(jìn)行圖形界面編程的方法。第2頁,共166頁。本章知識點(diǎn)8.1 常用Tkinter組件8.2 窗體布局8.3 Tkinter字體8.4 事件處理第3頁,共166頁。8.1 常用Tkinter組件8.1.1 彈出消息框8.1.2 創(chuàng)

2、建Windows窗口8.1.3 Label組件8.1.4 Button組件8.1.5 Canvas畫布組件8.1.6 Checkbutton組件8.1.7 Entry組件8.1.8 Frame組件8.1.9 Listbox組件8.1.10 Menu組件8.1.11 Radiobutton組件8.1.12 Scale組件8.1.13 Text組件第4頁,共166頁。8.1.1 彈出消息框用tkinter.messagebox模塊可以實(shí)現(xiàn)此功能。首先需要引入tkinter.messagebox模塊,具體如下:from tkinter.messagebox import *第5頁,共166頁。8.1

3、.1 彈出消息框使用showinfo()函數(shù)可以彈出提示消息框,方法如下showinfo(title=標(biāo)題,message=內(nèi)容)第6頁,共166頁?!纠?-1】 彈出一個(gè)提示消息框from tkinter.messagebox import *showinfo(title=提示,message=歡迎光臨)運(yùn)行程序會彈出如圖8-1所示的消息框。消息框左側(cè)有一個(gè)信息圖標(biāo)。第7頁,共166頁。2彈出警告消息框使用showwarning()函數(shù)可以彈出警告消息框,方法如下showwarning(title=標(biāo)題,message=內(nèi)容)第8頁,共166頁?!纠?-2】 彈出一個(gè)警告消息框from tk

4、inter.messagebox import *showwarning(title=提示,message=請輸入密碼)第9頁,共166頁。3彈出錯(cuò)誤消息框使用showerror()函數(shù)可以彈出錯(cuò)誤消息消息框,方法如下showerror(title=標(biāo)題,message=內(nèi)容)第10頁,共166頁?!纠?-3】 彈出一個(gè)警告消息框from tkinter.messagebox import *showerror(title=提示,message=密碼錯(cuò)誤)第11頁,共166頁。4彈出疑問消息框使用askquestion ()函數(shù)可以彈出一個(gè)包含“是”和“否”按鈕的疑問消息框,方法如下askqu

5、estion (title=標(biāo)題,message=內(nèi)容)如果用戶單擊“是” 按鈕,則askquestion ()函數(shù)返回YES(即字符串yes),如果用戶單擊“否” 按鈕,則askquestion ()函數(shù)返回NO(即字符串no)。第12頁,共166頁。【例8-4】 彈出一個(gè)疑問消息框from tkinter.messagebox import *ret=askquestion (title=請確認(rèn),message=是否刪除此用戶?)if ret=YES: showinfo(title=提示,message=已刪除)第13頁,共166頁。使用askyesnocancel()函數(shù)也可以使用ask

6、yesnocancel()函數(shù)彈出一個(gè)包含“是”和“否”按鈕的疑問消息框,方法如下askyesnocancel(title=標(biāo)題,message=內(nèi)容)與askquestion ()函數(shù)不同的是,如果用戶單擊“是” 按鈕,則askyesnocancel ()函數(shù)返回True,如果用戶單擊“否” 按鈕,則askyesnocancel()函數(shù)返回False。第14頁,共166頁。【例8-5】from tkinter.messagebox import *ret=askyesno(title=請確認(rèn),message=是否刪除此用戶?)if ret=True: showinfo(title=提示,me

7、ssage=刪除)第15頁,共166頁。5彈出帶確定和取消按鈕的疑問消息框使用askokcancel()函數(shù)可以彈出一個(gè)包含“確定”和“取消”按鈕的疑問消息框,方法如下askokcancel(title=標(biāo)題,message=內(nèi)容)如果用戶單擊“確定” 按鈕,則askokcancel()函數(shù)返回True,如果用戶單擊“取消”按鈕,則askokcancel()函數(shù)返回False。第16頁,共166頁。【例8-6】from tkinter.messagebox import *ret=askokcancel(title=請確認(rèn),message=是否確定繼續(xù)?)if ret=True: showin

8、fo(title=提示,message=繼續(xù))第17頁,共166頁。【例8-6】運(yùn)行程序會彈出如圖8-6所示的消息框。消息框左側(cè)有一個(gè)疑問圖標(biāo)。如果用戶單擊“確定”按鈕,則彈出如圖8-7所示的消息框。第18頁,共166頁。6彈出帶重試和取消按鈕的疑問消息框使用askretrycancel()函數(shù)可以彈出一個(gè)包含“重試”和“取消”按鈕的疑問消息框,方法如下askretrycancel(title=標(biāo)題,message=內(nèi)容)如果用戶單擊“重試” 按鈕,則askretrycancel()函數(shù)返回True,如果用戶單擊“取消” 按鈕,則askretrycancel()函數(shù)返回False。第19頁,共

9、166頁。【例8-7】from tkinter.messagebox import *ret=askokcancel(title=請確認(rèn),message=是否確定繼續(xù)?)if ret=True: showinfo(title=提示,message=重試)第20頁,共166頁。8.1.2 創(chuàng)建Windows窗口1導(dǎo)入Tkinter模塊在開發(fā)圖形用戶界面應(yīng)用程序之前首先應(yīng)該導(dǎo)入Tkinter模塊,代碼如下:from tkinter import *第21頁,共166頁。2創(chuàng)建窗口對象可以使用下面的方法創(chuàng)建一個(gè)Windows窗口對象:win = Tk();第22頁,共166頁。3顯示W(wǎng)indows窗

10、口在創(chuàng)建Wiundows窗口對象win后,可以使用下面的代碼顯示W(wǎng)indows窗口:win.mainloop();mainloop()方法的功能是進(jìn)入窗口的主循環(huán),也就是顯示窗口。第23頁,共166頁?!纠?-8】 顯示一個(gè)Windows窗口from tkinter import *win = Tk();win.mainloop();第24頁,共166頁。4設(shè)置窗口標(biāo)題在創(chuàng)建Windows窗口對象后,可以使用title()方法設(shè)置窗口的標(biāo)題,方法如下:窗口對象.title(標(biāo)題字符串)第25頁,共166頁?!纠?-9】from tkinter import *win = Tk();win.ti

11、tle(我的窗口)win.mainloop();第26頁,共166頁。5設(shè)置窗口大小在創(chuàng)建Windows窗口對象后,可以使用geometry()方法設(shè)置窗口的標(biāo)題,方法如下:窗口對象.geometry(size)參數(shù)size用于指定窗口大小,格式如下:寬度x高度注意,這里x不是乘號,而是字母x。沒錯(cuò),xyz的x。第27頁,共166頁?!纠?-10】from tkinter import *win = Tk();win. geometry(800 x600)win.mainloop();第28頁,共166頁。【例8-11】 from tkinter import *win = Tk();win.

12、geometry(800 x600)win.minsize(400,300)win.maxsize(1440,900)win.mainloop();第29頁,共166頁。8.1.3 Label組件1創(chuàng)建和顯示Label對象創(chuàng)建Label對象的基本方法如下: Label對象 = Label(Tkinter Windows窗口對象,text = Label組件顯示的文本)顯示Label對象的方法如下:Label對象.pack()第30頁,共166頁。【例8-12】 使用Label組件的簡單例子。from tkinter import *win = Tk();#創(chuàng)建窗口對象win.title(我的窗

13、口)#設(shè)置窗口標(biāo)題l = Label(win,text = 我是Label組件)#創(chuàng)建Label組件l.pack()#顯示Label組件win.mainloop();第31頁,共166頁。2使用Label組件顯示圖片第32頁,共166頁。第33頁,共166頁?!纠?-13】from tkinter import *win = Tk();#創(chuàng)建窗口對象win.title(我的窗口)#設(shè)置窗口標(biāo)題l1 = Label(win,bitmap = error)# 顯示錯(cuò)誤圖標(biāo)l1.pack()#顯示Label組件l2 = Label(win,bitmap = hourglass)# 顯示沙漏圖標(biāo)l2.

14、pack()#顯示Label組件l3 = Label(win,bitmap = info)# 顯示信息圖標(biāo)l3.pack()#顯示Label組件l4 = Label(win,bitmap = questhead)# 顯示信息圖標(biāo)l4.pack()#顯示Label組件l5 = Label(win,bitmap = question)# 顯示疑問圖標(biāo)l5.pack()#顯示Label組件l6 = Label(win,bitmap = warning)# 顯示警告圖標(biāo)l6.pack()#顯示Label組件l7 = Label(win,bitmap = gray12)# 顯示灰度背景圖標(biāo)gray12l

15、7.pack()#顯示Label組件第34頁,共166頁。l8 = Label(win,bitmap = gray25)# 顯示灰度背景圖標(biāo)gray25l8.pack()#顯示Label組件l9 = Label(win,bitmap = gray50)# 顯示灰度背景圖標(biāo)gray50l9.pack()#顯示Label組件l10 = Label(win,bitmap = gray75)# 顯示灰度背景圖標(biāo)gray75l10.pack()#顯示Label組件win.mainloop();第35頁,共166頁。設(shè)置窗口標(biāo)題第36頁,共166頁。【例8-14】from tkinter import *

16、win = Tk();#創(chuàng)建窗口對象win.title(我的窗口)#設(shè)置窗口標(biāo)題bm = PhotoImage(file = C:Python34LibidlelibIconsidle_48.png)label = Label(win,image = bm)label.bm = bmlabel.pack()#顯示Label組件win.mainloop();第37頁,共166頁。3設(shè)置Label組件的顏色【例8-15】 設(shè)置Label組件的前景色和背景色。from tkinter import *win = Tk();#創(chuàng)建窗口對象label = Label(win, fg = red,bg =

17、 blue, text=有顏色的字符串)label.pack()#顯示Label組件win.mainloop();第38頁,共166頁。其他常用的Lable組件屬性屬 性說明width寬度height高度compound指定文本與圖像如何在Label上顯示,缺省為None。 當(dāng)指定image/bitmap時(shí),文本(text)將被覆蓋,只顯示圖像。可以使用的值如下: left,圖像居左。 Right圖像居右。 top:圖像居上。 bottom:圖像居下。 center:文字覆蓋在圖像上wraplength指定多少單位后開始換行,用于多行顯示文本justify指定多行的對齊方式,可以使用的值為LE

18、FT(左對齊)或RIGHT(右對齊)ahchor指定文本(text)或圖像(bitmap/image)在Label中的顯示位置??捎弥等缦拢?e,垂直居中,水平居右; w,垂直居中,水平居左; n,垂直居上,水平居中 s,垂直居下,水平居中 ne,垂直居上,水平居右 se,垂直居下,水平居中 sw,垂直居下,水平居左 nw,垂直居上,水平居左 center垂直居中,水平居中第39頁,共166頁。8.1.4 Button組件1創(chuàng)建和顯示Button對象創(chuàng)建Button對象的基本方法如下: Button對象 = Button (Tkinter Windows窗口對象,text = Button組件

19、顯示的文本, command=單擊按鈕所調(diào)用的)顯示Button對象的方法如下:Button對象.pack()第40頁,共166頁?!纠?-16】 使用Button組件的簡單例子。from tkinter import *from tkinter.messagebox import *def CallBack(): showinfo(title=,message=點(diǎn)我干嘛)win = Tk();#創(chuàng)建窗口對象win.title(使用Button組件的簡單例子)#設(shè)置窗口標(biāo)題b = Button (win,text = 點(diǎn)我啊, command=CallBack)#創(chuàng)建Button組件b.pac

20、k()#顯示Button組件win.mainloop();第41頁,共166頁。【例8-16】的運(yùn)行結(jié)果第42頁,共166頁。2使用Button組件顯示圖片除了顯示文本,還可以使用image屬性和bm屬性顯示自定義圖片,方法如下:bm = PhotoImage(file = 文件名)b = Button (win,text = 點(diǎn)我啊, command=CallBack,image = bm)#創(chuàng)建Button組件b.bm = bm第43頁,共166頁?!纠?-17】from tkinter import *from tkinter.messagebox import *def CallBac

21、k(): showinfo(title=,message=點(diǎn)我干嘛)win = Tk();#創(chuàng)建窗口對象win.title(使用Button組件的簡單例子)#設(shè)置窗口標(biāo)題bm = PhotoImage(file = C:Python34LibidlelibIconsidle_48.png)b = Button (win,text = 點(diǎn)我啊, command=CallBack,image = bm)#創(chuàng)建Button組件b.bm = bmb.pack()#顯示Button組件win.mainloop();第44頁,共166頁。第45頁,共166頁。3設(shè)置Button組件的大小【例8-18】 設(shè)

22、置Button組件的大小。from tkinter import *from tkinter.messagebox import *def CallBack(): showinfo(title=,message=點(diǎn)我干嘛)win = Tk();#創(chuàng)建窗口對象b = Button (win,text = 點(diǎn)我啊, command=CallBack, width=100, height=50)#創(chuàng)建Button組件b.pack()#顯示Button組件第46頁,共166頁。【例8-18】的運(yùn)行結(jié)果第47頁,共166頁。其他常用的Button組件屬性屬 性說明bitmap指定按鈕上顯示的位圖comp

23、ound指定文本與圖像如何在Label上顯示,缺省為None。 當(dāng)指定image/bitmap時(shí),文本(text)將被覆蓋,只顯示圖像??梢允褂玫闹等缦拢?left,圖像居左。 Right圖像居右。 top:圖像居上。 bottom:圖像居下。 center:文字覆蓋在圖像上wraplength指定多少單位后開始換行,用于多行顯示文本bg設(shè)置背景顏色fg設(shè)置前景顏色state設(shè)置組件狀態(tài)??梢匀≈禐檎#∟ORMAL)、激活(ACTIVE)和禁用(DISABLED)bd設(shè)置按鈕的邊框大小;缺省為1或2個(gè)像素第48頁,共166頁?!纠?-19】from tkinter import *from

24、tkinter.messagebox import *win = Tk();#創(chuàng)建窗口對象b1 = Button (win,text = 粗邊框的按鈕,bd=5)b1.pack()#顯示Button組件b2 = Button (win,text = 不能點(diǎn)擊的按鈕,state= DISABLED)b2.pack()#顯示Button組件第49頁,共166頁?!纠?-19】的運(yùn)行結(jié)果第50頁,共166頁。8.1.5 Canvas畫布組件1創(chuàng)建和顯示Canvas對象可以使用下面的方法創(chuàng)建一個(gè)Canvas對象。Canvas對象 = Canvas (父窗口, 選項(xiàng), . )顯示Canvas對象的方法如

25、下。Canvas對象.pack()第51頁,共166頁。創(chuàng)建Canvas對象時(shí)的常用選項(xiàng)屬 性說明bd指定畫布的邊框?qū)挾?,單位是像素bg指定畫布的背景顏色confine指定畫布在滾動(dòng)區(qū)域外是否可以滾動(dòng)。默認(rèn)為True,表示不能滾動(dòng)cursor指定畫布中的鼠標(biāo)指針,例如arrow, circle, dotheight指定畫布的高度highlightcolor選中畫布時(shí)的背景色relief指定畫布的邊框樣式,可選值包括SUNKEN, RAISED, GROOVE, and RIDGEscrollregion指定畫布的滾動(dòng)區(qū)域的元組(w, n, e, s)width指定畫布的寬度第52頁,共166頁

26、。【例8-20】from tkinter import *root = Tk()cv = Canvas(root, bg = red, width = 200, height = 100) cv.pack()root.mainloop()第53頁,共166頁。【例8-20】的運(yùn)行結(jié)果第54頁,共166頁。2繪制線條使用create_line()方法可以創(chuàng)建一個(gè)線條對象,具體語法如下:line = canvas.create_line(x0, y0, x1, y1, ., xn, yn, 選項(xiàng))參數(shù)x0, y0, x1, y1, ., xn, yn是線段的端點(diǎn)第55頁,共166頁。創(chuàng)建矩形對象時(shí)

27、的常用選項(xiàng)屬 性說明width指定線段的寬度arrow指定直線是否使用箭頭,可選值如下: none,沒有箭頭; first,起點(diǎn)有箭頭; last,終點(diǎn)有箭頭; both,兩端都有箭頭arrowshape設(shè)置箭頭的形狀,由3個(gè)整數(shù)構(gòu)成,分別代表填充長度、箭頭長度和箭頭寬度。例如,arrowshape = 40 40 10fill指定線段的顏色dash指定線段為虛線第56頁,共166頁。【例8-21】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv. create

28、_line(10,10,100,80,width=2, dash=7)cv.pack()root.mainloop()第57頁,共166頁?!纠?-22】 from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100)cv.create_line(10, 10, 100, 10, arrow=none)cv.create_line(10, 20, 100, 20, arrow=first)cv.create_line(10, 30, 100, 30, arrow=last)cv.c

29、reate_line(10, 40, 100, 40, arrow=both)cv.pack()第58頁,共166頁。3繪制矩形Canvas對象. create_rectangle(矩形左上角的x坐標(biāo), 矩形左上角的y坐標(biāo), 矩形右下角的x坐標(biāo), 矩形右下角的y坐標(biāo), 選項(xiàng), .)第59頁,共166頁。創(chuàng)建矩形對象時(shí)的常用選項(xiàng)屬 性說明outline指定邊框顏色fill指定填充顏色width指定邊框的寬度dash指定邊框?yàn)樘摼€stipple使用指定自定義畫刷填充矩形第60頁,共166頁?!纠?-23】 from tkinter import *root = Tk()cv = Canvas(ro

30、ot, bg = white, width = 200, height = 100) cv.create_rectangle(10,10,100,80,outline = blue, fill = red, width=2)cv.pack()root.mainloop()第61頁,共166頁?!纠?-24】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_rectangle(10,10,100,80,outline = blue, fill = w

31、hite, width=2, dash=7)cv.pack()root.mainloop()第62頁,共166頁?!纠?-25】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_rectangle(10,10,100,80,outline = blue, fill = red, width=2, stipple = gray12,)cv.pack()root.mainloop()第63頁,共166頁。4繪制弧使用create_arc()方法可以創(chuàng)建

32、一個(gè)弧對象,可以是一個(gè)和弦、餅圖扇區(qū)或者一個(gè)簡單的弧、具體語法如下:Canvas對象. create_arc(弧外框矩形左上角的x坐標(biāo), 弧外框矩形左上角的y坐標(biāo), 弧外框矩形右下角的x坐標(biāo), 弧外框矩形右下角的y坐標(biāo), 選項(xiàng), .)第64頁,共166頁。創(chuàng)建弧對象時(shí)的常用選項(xiàng)屬 性說明outline指定邊框顏色fill指定填充顏色width指定邊框的寬度start起始弧度extent終止弧度第65頁,共166頁。【例8-26】 繪制一個(gè)指定30度的弧from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 20

33、0, height = 100) cv.create_arc(10,10,100,80,outline = blue, fill = red, width=2, start=0, extent =30)cv.pack()root.mainloop()第66頁,共166頁。運(yùn)行結(jié)果第67頁,共166頁?!纠?-27】 繪制一個(gè)圓inter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_arc(10,10,100,100,outline = blue, fill = red

34、, width=2,start=0, extent =359)cv.pack()root.mainloop()第68頁,共166頁。5繪制多邊形使用create_polygon()方法可以創(chuàng)建一個(gè)多邊形對象,可以是一個(gè)三角形、矩形或者任意一個(gè)多邊形,具體語法如下:Canvas對象. create_polygon (頂點(diǎn)1的x坐標(biāo), 頂點(diǎn)1的y坐標(biāo), 頂點(diǎn)2的x坐標(biāo), 頂點(diǎn)2的y坐標(biāo), , 頂點(diǎn)n的x坐標(biāo), 頂點(diǎn)n的y坐標(biāo), 選項(xiàng), .)第69頁,共166頁。創(chuàng)建多邊形對象時(shí)的常用選項(xiàng)屬 性說明outline指定邊框顏色fill指定填充顏色width指定邊框的寬度smooth指定多邊形的平滑程度

35、。等于0表示多邊形的邊是折線。等于1表示多邊形的邊是平滑曲線第70頁,共166頁?!纠?-28】 繪制一個(gè)三角形。from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_polygon (100,5,0,80,200,80, outline = blue, fill = red, width=2)cv.pack()root.mainloop()第71頁,共166頁?!纠?-28】的運(yùn)行結(jié)果第72頁,共166頁。6繪制橢圓使用create_oval()方法

36、可以創(chuàng)建一個(gè)橢圓對象,具體語法如下:Canvas對象. create_oval(包裹橢圓的矩形的左上角x坐標(biāo), 包裹橢圓的矩形的左上角y坐標(biāo), 橢圓的長度, 橢圓的長度, 選項(xiàng), .)第73頁,共166頁。創(chuàng)建橢圓對象時(shí)的常用選項(xiàng)屬 性說明outline指定邊框顏色fill指定填充顏色width指定邊框的寬度第74頁,共166頁?!纠?-30】 繪制一個(gè)長100、寬80的橢圓。from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_oval (10,10

37、,100,50, outline = blue, fill = red, width=2)cv.pack()root.mainloop()第75頁,共166頁?!纠?-30】的運(yùn)行結(jié)果第76頁,共166頁?!纠?-31】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_oval (10,10,100,100, outline = blue, fill = red, width=2)cv.pack()root.mainloop()第77頁,共166頁

38、?!纠?-31】的運(yùn)行結(jié)果第78頁,共166頁。7繪制文字使用create_text()方法可以創(chuàng)建一個(gè)文字對象,具體語法如下:文字對象 = Canvas對象.create_text(文本左上角的x坐標(biāo),文本左上角的y坐標(biāo)), 選項(xiàng), .)第79頁,共166頁。創(chuàng)建文字對象時(shí)的常用選項(xiàng)屬 性說明text文字對象的文本內(nèi)容fill指定文字顏色anchor控制文字對象的位置,w表示左對齊,e表示右對齊,n表示頂對齊,s表示底對齊,nw表示左上對齊, sw表示左下對齊, se表示右下對齊, ne表示右上對齊, center表示居中對齊。默認(rèn)值為centerjustify設(shè)置文字對象中文本的對齊方式。

39、left表示左對齊,right表示右對齊,center表示居中對齊。默認(rèn)值為center第80頁,共166頁?!纠?-32】 繪制一段文字from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) cv.create_text(10,10), text = Hello Python, fill = red, anchor=nw)cv.create_text(10,20), text = 你好,Python!, fill = blue, anchor=se)cv.pack()ro

40、ot.mainloop()第81頁,共166頁?!纠?-32】的運(yùn)行結(jié)果第82頁,共166頁。select_from()方法和select_to()方法select_from()方法用于指定選中文本的起始位置,具體用法如下:Canvas對象. select_from(文字對象, 選中文本的起始位置)select_to()方法用于指定選中文本的結(jié)束位置,具體用法如下:Canvas對象. select_from(文字對象, 選中文本的結(jié)束位置)第83頁,共166頁?!纠?-33】 選中文字的例子from tkinter import *root = Tk()cv = Canvas(root, bg

41、 = white, width = 200, height = 100) txt = cv.create_text(10,10), text = Hello Python, fill = red, anchor=nw)# 設(shè)置文本的選中起始位置 cv.select_from(txt,6) # 設(shè)置文本的選中結(jié)束位置cv.select_to(txt,11)cv.pack()root.mainloop()第84頁,共166頁?!纠?-33】的運(yùn)行結(jié)果第85頁,共166頁。8繪制圖像使用create_bitmap()方法可以繪制Python內(nèi)置的位圖,具體方法如下:Canvas對象. create_

42、bitmap(位圖左上角的x坐標(biāo), 位圖左上角的y坐標(biāo)),bitmap =位圖字符串)第86頁,共166頁?!纠?-34】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) d = 1:error,2:info,3:question,4:hourglassfor in d: cv.create_bitmap(20*i,20*i),bitmap = di)cv.pack()root.mainloop()第87頁,共166頁?!纠?-34】的運(yùn)行結(jié)果第88頁,共166頁?!?/p>

43、例8-35】from tkinter import *root = Tk()cv = Canvas(root, bg = white, width = 200, height = 100) img = PhotoImage(file = C:Python34LibidlelibIconspython.gif)cv.create_bitmap(80,20*i), image = img)cv.pack()root.mainloop()第89頁,共166頁?!纠?-35】的運(yùn)行結(jié)果第90頁,共166頁。9修改圖形對象的坐標(biāo)使用coords()方法可以修改圖形對象的坐標(biāo),具體方法如下:Canvas對

44、象.coords(圖形對象, (圖形左上角的x坐標(biāo), 圖形左上角的y坐標(biāo), 圖形右下角的x坐標(biāo), 圖形右下角的y坐標(biāo))第91頁,共166頁?!纠?-36】from tkinter import *root = Tk()# 創(chuàng)建一個(gè)Canvas,設(shè)置其背景色為白色cv = Canvas(root, bg = white, width = 200, height = 100) rt= cv.create_rectangle(10,10,110,110,outline=red,stipple=gray12,fill=green)cv.pack()# 重新設(shè)置rt的坐標(biāo)cv.coords(rt,(40

45、,40,200,100)root.mainloop()第92頁,共166頁。第93頁,共166頁。10移動(dòng)指定圖形對象使用move()方法可以修改圖形對象的坐標(biāo),具體方法如下:Canvas對象. move (圖形對象, x坐標(biāo)偏移量, y坐標(biāo)偏移量)第94頁,共166頁?!纠?-37】from tkinter import *root = Tk()# 創(chuàng)建一個(gè)Canvas,設(shè)置其背景色為白色 cv = Canvas(root, bg = white, width = 200, height = 100) rt1 = cv.create_rectangle(10,10,110,110,outli

46、ne=red,stipple=gray12,fill=green)cv.pack()rt2 = cv.create_rectangle(10,10,110,110,outline=blue)# 移動(dòng)rt1 cv.move(rt1,20,-10)cv.pack()root.mainloop()第95頁,共166頁。沒有cv.coords()語句時(shí)【例8-37】的運(yùn)行結(jié)果第96頁,共166頁。11刪除圖形對象from tkinter import *root = Tk()# 創(chuàng)建一個(gè)Canvas,設(shè)置其背景色為白色 cv = Canvas(root, bg = white, width = 200

47、, height = 100) rt1 = cv.create_rectangle(10,10,110,110,outline=red,stipple=gray12, fill=green)cv.pack()rt2 = cv.create_rectangle(10,10,110,110,outline=blue)# 刪除rt1 cv.delete(rt1)cv.pack()root.mainloop()第97頁,共166頁?!纠?-38】的運(yùn)行結(jié)果第98頁,共166頁。12縮放圖形對象使用scale()方法可以縮放圖形對象,具體方法如下:Canvas對象.scale(圖形對象, x軸偏移量,y

48、軸偏移量,x軸縮放比例,y軸縮放比例)第99頁,共166頁。【例8-39】 縮放一個(gè)矩形對象from tkinter import *root = Tk()# 創(chuàng)建一個(gè)Canvas,設(shè)置其背景色為白色 cv = Canvas(root, bg = white, width = 200, height = 300) rt1 = cv.create_rectangle(10,10,110,110,outline=red,stipple=gray12, fill=green)cv.scale(rt1,0,0,1,2)cv.pack()root.mainloop()第100頁,共166頁?!纠?-38

49、】的運(yùn)行結(jié)果第101頁,共166頁。13為圖形對象指定標(biāo)記(tag)在創(chuàng)建圖形對象時(shí)可以使用屬性tags設(shè)置圖形對象的標(biāo)記(tag),例如:rt = cv.create_rectangle(10,10,110,110, tags = r1)上面的語句指定矩形對象rt具有一個(gè)tag r1。也可以同時(shí)設(shè)置多個(gè)標(biāo)記(tag),例如:rt = cv.create_rectangle(10,10,110,110, tags = (r1,r2,r3)第102頁,共166頁。find_withtag()方法使用find_withtag()方法可以獲取到指定tag的圖形對象,然后設(shè)置圖形對象的屬性。find_

50、withtag()方法的語法如下:Canvas對象.find_withtag(tag名)find_withtag()方法返回一個(gè)圖形對象數(shù)組,其中包含所有具有tag名的圖形對象。使用find_withtag()方法可以設(shè)置圖形對象的屬性,語法如下:Canvas對象. find_withtag(圖形對象, 屬性1=值1, 屬性2=值2 )第103頁,共166頁?!纠?-40】 使用屬性tags設(shè)置圖形對象標(biāo)記的例子from tkinter import *root = Tk()# 創(chuàng)建一個(gè)Canvas,設(shè)置其背景色為白色 cv = Canvas(root, bg = white, width =

51、 200, height = 200) # 使用tags指定一個(gè)tag(r1) rt = cv.create_rectangle(10,10,110,110, tags = (r1,r2,r3)cv.pack()cv.create_rectangle(20,20,80,80, tags = r3)# 將所有與tag(r3)綁定的item邊框顏色設(shè)置為藍(lán)色 for item in cv.find_withtag(r3): cv.itemconfig(item,outline = blue)第104頁,共166頁?!纠?-40】的運(yùn)行結(jié)果第105頁,共166頁。8.1.6 Checkbutton組

52、件1創(chuàng)建和顯示Button對象創(chuàng)建Checkbutton對象的基本方法如下: Checkbutton對象 = Checkbutton(Tkinter Windows窗口對象,text = Checkbutton組件顯示的文本, command=單擊Checkbutton按鈕所調(diào)用的回調(diào)函數(shù))顯示Checkbutton對象的方法如下:Checkbutton對象.pack()第106頁,共166頁?!纠?-41】from tkinter import *from tkinter.messagebox import *def CallBack(): showinfo(title=,message=點(diǎn)

53、我干嘛)win = Tk();#創(chuàng)建窗口對象win.title(使用Checkbutton組件的簡單例子)#設(shè)置窗口標(biāo)題b = Checkbutton (win,text = Python Tkinter, command=CallBack)#創(chuàng)建Checkbutton組件b.pack()#顯示Checkbutton組件win.mainloop();第107頁,共166頁?!纠?-41】的運(yùn)行結(jié)果第108頁,共166頁。2獲取Checkbutton組件是否被選中Checkbutton組件有On和Off兩個(gè)狀態(tài),值,缺省狀態(tài)下On為1,Off為0。也可以使用onvalue 設(shè)置Checkbutt

54、on組件被選中時(shí)的值,使用offvalue 可以設(shè)置Checkbutton組件被取消選中時(shí)的值,使用例如:Checkbutton(root, text = 喜歡, onvalue = 1, offvalue = 0,command = CallBack).pack() root.mainloop()為了獲取Checkbutton組件是否被選中,需要使用variable 屬性為Checkbutton組件指定一個(gè)對應(yīng)的變量,例如:v = StringVar()Checkbutton(root, variable =v, text = 喜歡, onvalue = 1, offvalue = 0,co

55、mmand = CallBack).pack()root.mainloop()第109頁,共166頁。【例8-42】 使用一個(gè)Button組件獲取Checkbutton組件的狀態(tài)from tkinter import *root = Tk()#將一字符串與Checkbutton的值綁定,每次點(diǎn)擊Checkbutton,將打印出當(dāng)前的值v = StringVar()def callCheckbutton(): print(v.get()第110頁,共166頁。Checkbutton(root, variable = v, text = checkbutton value, onvalue = p

56、ython, #設(shè)置On的值 offvalue = tkinter, #設(shè)置Off的值 command = callCheckbutton).pack()b = Button (root,text = 獲取Checkbutton狀態(tài), command=callCheckbutton, width=20)#創(chuàng)建Button組件v.set(python)b.pack()#顯示Button組件root.mainloop()第111頁,共166頁。8.1.7 Entry組件1創(chuàng)建和顯示Entry對象創(chuàng)建Entry對象的基本方法如下: Entry對象 = Entry (Tkinter Windows窗口

57、對象)顯示Entry對象的方法如下:Entry對象.pack()第112頁,共166頁。【例8-42】 使用Entry組件的簡單例子。from tkinter import *win = Tk();#創(chuàng)建窗口對象win.title(使用Entry組件的簡單例子)#設(shè)置窗口標(biāo)題entry = Entry (win)#創(chuàng)建Entry組件entry.pack()#顯示Entry組件win.mainloop();第113頁,共166頁?!纠?-42】的運(yùn)行結(jié)果第114頁,共166頁。2獲取Entry組件的內(nèi)容e = StringVar()Checkbutton(root, textvariable =

58、e).pack()root.mainloop()第115頁,共166頁?!纠?-43】from tkinter import *root = Tk()#將一字符串與Entry的值綁定e = StringVar()def callbutton(): print(e.get()第116頁,共166頁。root.title(使用Entry組件的簡單例子)#設(shè)置窗口標(biāo)題entry = Entry (root, textvariable = e).pack()b = Button (root,text = 獲取Entry組件的內(nèi)容, command=callbutton, width=20)#創(chuàng)建But

59、ton組件e.set(python)b.pack()#顯示Button組件root.mainloop()第117頁,共166頁。8.1.8 Frame組件1創(chuàng)建和顯示Frame對象創(chuàng)建Frame對象的基本方法如下: Frame對象 = Frame (Tkinter Windows窗口對象, height = 高度,width = 寬度,bg = 背景色)顯示Entry對象的方法如下:Frame對象.pack()第118頁,共166頁?!纠?-44】 使用Frame組件的簡單例子。from tkinter import *win = Tk();#創(chuàng)建窗口對象win.title(使用Frame組件

60、的簡單例子)#設(shè)置窗口標(biāo)題fm = Frame (win, height = 20,width = 400,bg =green)#創(chuàng)建Frame組件fm.pack()#顯示Frame組件win.mainloop();第119頁,共166頁。【例8-44】的運(yùn)行結(jié)果第120頁,共166頁。2向Frame組件中添加組件在創(chuàng)建組件時(shí)可以指定其容器為Frame組件,例如:Label(Frame對象,text = Hello).pack()第121頁,共166頁?!纠?-45】from tkinter import *win = Tk();#創(chuàng)建窗口對象win.title(使用Frame組件的例子)#設(shè)

溫馨提示

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

最新文檔

評論

0/150

提交評論