1 說明:
1.1 推薦指數:★★★★
1.2 網上的一段代碼,來源不明,先表示感謝,如有侵權請告知,定會刪除。
1.3 對這段代碼進行調整、逐步優化和分析,很python。
1.4 python的相關知識點:self和列表推導式。
1.5 有點長,適合慢慢品味,提高自己的python思維。
2 效果圖
3 源代碼:
<code> import tkinter import time import threadingclass
choujiang
:def
__init__
(
self
):self
.root = tkinter.Tk()self
.root.title('lowB版轉盤'
)self
.root.minsize(300
,300
)self
.isloop = Falseself
.newloop = Falseself
.setwindow()self
.root.mainloop()def
setwindow
(
self
) :self
.btn_start = tkinter.Button(self
.root, text ='start/stop'
,command =self
.newtask)self
.btn_start.place(x=90
, y=125
, width=50
, height=50
)self
.btn1 = tkinter.Button(self
.root, text='趙'
, bg='red'
)self
.btn1.place(x=20
, y=20
, width=50
, height=50
)self
.btn2 = tkinter.Button(self
.root, text='錢'
, bg='white'
)self
.btn2.place(x=90
, y=20
, width=50
, height=50
)self
.btn3 = tkinter.Button(self
.root, text='孫'
, bg='white'
)self
.btn3.place(x=160
, y=20
, width=50
, height=50
)self
.btn4 = tkinter.Button(self
.root, text='李'
, bg='white'
)self
.btn4.place(x=230
, y=20
, width=50
, height=50
)self
.btn5 = tkinter.Button(self
.root, text='周'
, bg='white'
)self
.btn5.place(x=230
, y=90
, width=50
, height=50
)self
.btn6 = tkinter.Button(self
.root, text='吳'
, bg='white'
)self
.btn6.place(x=230
, y=160
, width=50
, height=50
)self
.btn7 = tkinter.Button(self
.root, text='鄭'
, bg='white'
)self
.btn7.place(x=230
, y=230
, width=50
, height=50
)self
.btn8 = tkinter.Button(self
.root, text='王'
, bg='white'
)self
.btn8.place(x=160
, y=230
, width=50
, height=50
)self
.btn9 = tkinter.Button(self
.root, text='馮'
, bg='white'
)self
.btn9.place(x=90
, y=230
, width=50
, height=50
)self
.btn10
= tkinter.Button(self
.root, text='陳'
, bg='white'
)self
.btn10
.place(x=20
, y=230
, width=50
, height=50
)self
.btn11 = tkinter.Button(self
.root, text='褚'
, bg='white'
)self
.btn11.place(x=20
, y=160
, width=50
, height=50
)self
.btn12 = tkinter.Button(self
.root, text='衛'
, bg='white'
)self
.btn12.place(x=20
, y=90
, width=50
, height=50
)self
.girlfrends = [self
.btn1,self
.btn2,self
.btn3,self
.btn4,self
.btn5,self
.btn6,self
.btn7,self
.btn8,self
.btn9,self
.btn10
,self
.btn11,self
.btn12]def
rounds
(
self
):if
self
.isloop ==True:
return
i =0
while
True:
if
self
.newloop ==True:
self
.newloop = Falsereturn
time.sleep(0
.1
)for
xin
self
.girlfrends:
x['bg'
] ='white'
self
.girlfrends[i]['bg'
] ='red'
i +=1
if
i >= len(self
.girlfrends): i =0
def
newtask
(
self
):if
self
.isloop ==False:
t = threading.Thread(target =self
.rounds) t.start()self
.isloop = True elifself
.isloop ==True:
self
.isloop = Falseself
.newloop = True c = choujiang()/<code>
4 上面牽涉到:
4.1 Python中的類和實例:
面向對象最重要的概念就是類(class)和實例(instance),類是抽象的模板。
由於類起到模板的作用,因此,可以在創建實例的時候,把我們認為必須綁定的屬性強制填寫進去。
4.2 Python的self用法:
Python當中的一個內置方法__init__方法,而__init__方法的第一參數永遠是self,表示創建的類實例本身。
比如格式:
<code>class
Good
(object
):def
__init__
(
self
, name, score):self
.name = nameself
.score = score/<code>
python的self和JavaScript(包括vue)的this很像,多指代當前屬性,由於大型項目中可能有很多屬性名重複,可是值不一定相同,為了避免報錯,推薦這種指代性的屬性。基於此,考慮小型抽獎tk,所以決定去掉self。
5 去掉self的代碼:
<code>import
tkinterimport
timeimport
threading root = tkinter.Tk() root.title('lowB版轉盤'
) root.minsize(300
,300
) isloop =False
newloop =False
def
newtask
()
:global
isloopglobal
newloopif
isloop ==False
: t = threading.Thread(target = rounds) t.start() isloop =True
elif
isloop ==True
: isloop =False
newloop =True
btn_start = tkinter.Button(root, text ='start/stop'
,command = newtask) btn_start.place(x=90
, y=125
, width=50
, height=50
) btn1 = tkinter.Button(root, text='趙'
, bg='red'
) btn1.place(x=20
, y=20
, width=50
, height=50
) btn2 = tkinter.Button(root, text='錢'
, bg='white'
) btn2.place(x=90
, y=20
, width=50
, height=50
) btn3 = tkinter.Button(root, text='孫'
, bg='white'
) btn3.place(x=160
, y=20
, width=50
, height=50
) btn4 = tkinter.Button(root, text='李'
, bg='white'
) btn4.place(x=230
, y=20
, width=50
, height=50
) btn5 = tkinter.Button(root, text='周'
, bg='white'
) btn5.place(x=230
, y=90
, width=50
, height=50
) btn6 = tkinter.Button(root, text='吳'
, bg='white'
) btn6.place(x=230
, y=160
, width=50
, height=50
) btn7 = tkinter.Button(root, text='鄭'
, bg='white'
) btn7.place(x=230
, y=230
, width=50
, height=50
) btn8 = tkinter.Button(root, text='王'
, bg='white'
) btn8.place(x=160
, y=230
, width=50
, height=50
) btn9 = tkinter.Button(root, text='馮'
, bg='white'
) btn9.place(x=90
, y=230
, width=50
, height=50
) btn10 = tkinter.Button(root, text='陳'
, bg='white'
) btn10.place(x=20
, y=230
, width=50
, height=50
) btn11 = tkinter.Button(root, text='褚'
, bg='white'
) btn11.place(x=20
, y=160
, width=50
, height=50
) btn12 = tkinter.Button(root, text='衛'
, bg='white'
) btn12.place(x=20
, y=90
, width=50
, height=50
) girlfrends = [btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12]def
rounds
()
:global
newloopif
isloop ==True
:return
i =0
while
True
:if
newloop ==True
: newloop =False
return
time.sleep(0.1
)for
xin
girlfrends: x['bg'
] ='white'
girlfrends[i]['bg'
] ='red'
i +=1
if
i >= len(girlfrends): i =0
root.mainloop()/<code>
6 注意去掉self和類定義後,代碼的修改的位置,可能要改變,仔細比對上面的代碼1和2的區別。
6.1 由於開始和暫定按鈕的命令:newtask,所以需要把定義函數newtask,放在按鈕設置的前面。
6.2 增加全局變量,global isloop和global newloop,注意仔細分析。python的局部變量和全局變量的關係,可以複習複習。
==========================高級一點==================
7 發現上面的按鈕設置的代碼很多重複的,以前我講過,重複多的事情,python可以推導式。
簡潔的推導式代碼如下:
<code>import
tkinterimport
timeimport
threading root = tkinter.Tk() root.title('lowB版轉盤'
) root.minsize(300
,300
) isloop =False
newloop =False
def
newtask
()
:global
isloopglobal
newloopif
isloop ==False
: t = threading.Thread(target = rounds) t.start() isloop =True
elif
isloop ==True
: isloop =False
newloop =True
btn_start = tkinter.Button(root, text ='start/stop'
,command = newtask) btn_start.place(x=90
, y=125
, width=50
, height=50
) n=['趙'
,'錢'
,'孫'
,'李'
,'周'
,'吳'
,'鄭'
,'王'
,'馮'
,'陳'
,'褚'
,'衛'
] c=['red'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
,'white'
] x=[20
,90
,160
,230
,230
,230
,230
,160
,90
,20
,20
,20
] y=[20
,20
,20
,20
,90
,160
,230
,230
,230
,230
,160
,90
] z=50
girlfrends =[]for
iin
range(12
): girlfrends.append('btn'
+str(i+1
)) girlfrends[i]=tkinter.Button(root,text=n[i],bg=c[i]) girlfrends[i].place(x=x[i],y=y[i],width=z,height=z) i+=1
def
rounds
()
:global
newloopif
isloop ==True
:return
i =0
while
True
:if
newloop ==True
: newloop =False
return
time.sleep(0.1
)for
xin
girlfrends: x['bg'
] ='white'
girlfrends[i]['bg'
] ='red'
i +=1
if
i >= len(girlfrends): i =0
root.mainloop()/<code>
自己整理出來,分享給大家。