最近上課學生多名字記不住,名冊忘記了帶,要點名怎么辦,好久沒有寫代碼了,于是自己寫了個點名軟件,記錄下吧,第一次接觸TK也不是太熟悉,寫的不太好,記錄下源代碼 以后遇到要寫桌面軟件還是可以耍耍的。
?
tk:文檔? https://wiki.python.org/moin/TkInter
tk是python 自帶的一個GUI模塊
效果:
?
背景圖:
?
icon圖標:
?
源碼:
from
win32com.client
import
Dispatch
from
tkinter
import
*
import
tkinter as tk
from
PIL
import
Image
from
PIL
import
ImageTk
import
os
import
re
import
random
from
threading
import
Thread
import
pythoncom
import
time
stu_path
=
"
名單.txt
"
#
學生名單路徑
def
speaker(str):
"""
語音播報
:param str: 需要播放語音的文字
"""
speaker
= Dispatch(
"
SAPI.SpVoice
"
)
speaker.Speak(str)
class
Rollllcall():
def
__init__
(self):
self.win
=
Tk()
self.win.title(
"
Python課堂點名器
"
)
self.win.iconbitmap(
"
image/icon.ico
"
)
self.win.geometry(
"
750x450
"
)
self.win.resizable(False, False)
#
不允許放大窗口,避免放大導致布局變形帶來的麻煩
self.start = False
#
開始按鈕的狀態
#
增加背景圖片
img = Image.open(
'
image/back.jpg
'
)
img
= ImageTk.PhotoImage(img, size=(650, 450
))
theLabel
= tk.Label(self.win,
#
綁定到一個框架
#
justify=tk.LEFT, # 對齊方式
image=img,
#
加入圖片
compound=tk.CENTER,
#
關鍵:設置為背景圖片
font=(
"
華文行楷
"
, 20),
#
字體和字號
fg=
"
white
"
,
)
#
前景色
theLabel.place(x=0, y=0, relwidth=1, relheight=1
)
self.var
= tk.StringVar()
#
儲存文字的類
self.var.set(
"
別緊張
"
)
#
設置文字
NameLabel = tk.Label(self.win, textvariable=self.var,
#
綁定到一個框架
justify=tk.LEFT,
#
對齊方式
compound=tk.CENTER,
#
關鍵:設置為背景圖片
font=(
"
華文行楷
"
, 35),
#
字體和字號
fg=
"
SeaGreen
"
,
width
=10
,
)
#
前景色
NameLabel.place(x=280, y=100
)
#
多選框
self.checkVar =
IntVar()
Checkbutton(self.win, text
=
"
語音播放
"
, variable=
self.checkVar,
onvalue
=1, offvalue=0, height=0, width=0).place(x=170, y=410
)
tk.Button(self.win, text
=
'
編輯學生名單
'
, height=0, width=0, command=self.pop_win).place(x=520, y=408
)
self.theButton
= tk.Button(self.win, text=
"
開始
"
, font=(
"
華文行楷
"
, 13), fg=
"
SeaGreen
"
, width=20
,
command
=
self.callback)
self.theButton.place(x
=300, y=360)
#
調整按鈕的位置
self.win.mainloop()
def
save_names(self, pop, t):
"""
保存名單內容
:param win: #彈出窗
:param t: 文本框對象
"""
names
= t.get(0.0,
"
end
"
)
if
re.search(
"
,
"
, names):
textlabel
= tk.Label(pop, text=
"
注意:名單不能使用中文逗號分隔
"
, font=(
"
華文行楷
"
, 12),
#
字體和字號
fg=
"
red
"
, )
textlabel.place(y
=190, x=10
)
else
:
with open(stu_path,
"
w
"
, encoding=
"
utf-8
"
) as f:
f.write(names)
pop.destroy()
#
編輯學生姓名
def
pop_win(self):
pop
= Tk(className=
'
學生名單編輯
'
)
#
彈出框框名
pop.geometry(
'
450x250
'
)
#
設置彈出框的大小 w x h
pop.iconbitmap(
"
image/icon.ico
"
)
pop.resizable(False, False)
#
用來編輯名單的文本框
t = tk.Text(pop, width=61, height=
'
10
'
)
t.place(x
=10, y=10
)
#
判斷文件存不存在
result =
os.path.exists(stu_path)
if
result:
#
存在
with open(stu_path,
"
r
"
, encoding=
'
utf-8
'
) as f:
names
= f.read().strip(
"
\n\r\t
"
)
t.insert(
"
end
"
, names)
textlabel
= tk.Label(pop, text=
"
學生名單請以,(英文狀態)的逗號分隔:\n如:劉亦菲,周迅
"
, font=(
"
華文行楷
"
, 12),
#
字體和字號
fg=
"
SeaGreen
"
, )
textlabel.place(y
=150, x=10
)
#
點擊確定保存數據
tk.Button(pop, text=
'
確定
'
, height=0, width=0, command=
lambda
: self.save_names(pop, t)).place(y=200, x=340
)
tk.Button(pop, text
=
'
取消
'
, height=0, width=0, command=pop.destroy).place(y=200, x=400
)
def
callback(self):
#
改變開始按鈕的狀態
self.start = False
if
self.start
else
True
#
開始隨機名單之后修改按鈕上的文字
self.theButton[
"
text
"
] =
"
就你了
"
#
開啟一個子線程去做操作隨機名字,以及語言播報
self.t = Thread(target=self.mod_stu_name, args=
(self.var, self.checkVar))
self.t.start()
def
mod_stu_name(self, var, checkVar):
#
隨機讀取名單中的一個
pythoncom.CoInitialize()
#
子線程中調用win32com 語音播放需要設置這一行
if
not
os.path.exists(stu_path):
var.set(
"
請添加名單
"
)
return
None
with open(stu_path,
"
r
"
, encoding=
"
utf-8
"
) as f:
names
= f.read().strip(
"
\n\t\r,
"
)
if
not
names:
var.set(
"
請添加名單
"
)
return
None
name_list
= names.split(
"
,
"
)
random_name
=
""
while
self.start:
random_name
=
random.choice(name_list)
var.set(random_name)
#
設置名字隨機出現
time.sleep(0.1
)
self.theButton[
"
text
"
] =
"
開始
"
#
選中之后將按鈕重新修改成 開始
#
語音播報
if
checkVar.get() == 1
:
speaker(random_name)
if
__name__
==
'
__main__
'
:
Rollllcall()
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

