對(duì)于喜歡用電腦的人來說,
一旦往那里一坐,就很容易忘記時(shí)間,
接觸到Python的我們,當(dāng)然是要自己DIY一個(gè)鬧鐘啦,而且一定要醒目!
這個(gè)時(shí)候我就想到了tkinter
1- 需要的庫:
- tkinter
- datetime
- time
都是Python系統(tǒng)自帶的庫,這里推薦用Python3
2- tkinter最簡單的使用
import tkinter
root = tkinter.Tk()
root.mainloop()
導(dǎo)入tkinter,實(shí)例化一個(gè)Tk類,進(jìn)入主循環(huán),完成,一個(gè)窗口就創(chuàng)建好了,
像下面這樣:
3- 無邊框tkinter窗口的創(chuàng)建
root.overrideredirect(True)
把窗口的邊框給去掉,用上面這個(gè)函數(shù),參數(shù)設(shè)置為True就可以了
root.geometry("{0}x{1}+0+0".format(
root.winfo_screenwidth(),
root.winfo_screenheight()))
把窗口的長寬設(shè)置為屏幕的長寬,這樣就能鋪滿整個(gè)屏幕,醒目!
# 這里用來設(shè)定字體,字體本身為黑色,字體背景為白色
tkinter.Label(root, text=word_text, fg='black', bg='white',
# 字體是微軟雅黑, 大小是100
font=('Microsoft YaHei', 100)
# 把label這個(gè)控件居中
).place(anchor='center',
relx=0.5,
rely=0.5)
label控件一般是用來顯示文字的,把label居中,
字體設(shè)置為微軟雅黑,大小是100,字體顏色是黑色,label控件的顏色為白色
4- 時(shí)間的獲取
import datetime
def get_time():
hour = datetime.datetime.now().hour
minute = datetime.datetime.now().minute
return hour, minute
鬧鐘就是在固定的時(shí)間提醒你,
所以要獲取現(xiàn)在的時(shí)間,和我們?cè)O(shè)定的時(shí)間進(jìn)行對(duì)比,
到了時(shí)間,彈出窗口,達(dá)到提醒的效果
5- 代碼
import datetime
import time
import tkinter
HOUR = 20 # 幾點(diǎn)
MINUTE = 38 # 幾分
MESSAGE = "是時(shí)候睡覺了!"
TIMES = 5 # 提醒多少次
INTERVAL = 2 # 每次彈窗窗口間隔的秒數(shù)
class Reminder(object):
def __init__(self, word_text):
# 初始化
self.root = tkinter.Tk()
# 把窗口的邊框給去掉
self.root.overrideredirect(True)
# 設(shè)置窗口的大小
self.root.geometry("{0}x{1}+0+0".format(
self.root.winfo_screenwidth(),
self.root.winfo_screenheight()))
# 設(shè)置整個(gè)窗口的背景為白色
self.root.configure(bg='white')
# 這里用來設(shè)定字體,字體本身為黑色,字體背景為白色
tkinter.Label(self.root, text=word_text, fg='black', bg='white',
# 字體是微軟雅黑, 大小是100
font=('Microsoft YaHei', 100)
# label這個(gè)控件居中
).place(anchor='center',
relx=0.5,
rely=0.5)
# 在窗口創(chuàng)建之后執(zhí)行 self.show 這個(gè)函數(shù)
# 這里這樣處理,才能達(dá)到后面窗口顯示2秒再消失的效果
self.root.after_idle(self.show)
def hide(self):
"""
隱藏窗口并退出
"""
self.root.withdraw()
self.root.quit()
def show(self):
"""
顯示窗口,持續(xù)2秒鐘
"""
self.root.deiconify()
self.root.after(1000 * 2, self.hide)
def start(self):
"""
進(jìn)入死循環(huán),這樣窗口才能一直存在
"""
self.root.mainloop()
def get_time():
hour = datetime.datetime.now().hour
minute = datetime.datetime.now().minute
return hour, minute
def prompt():
"""
彈出窗口
"""
root = Reminder(MESSAGE)
root.start()
time.sleep(INTERVAL)
def main():
# 這里的while循環(huán)是為了保證時(shí)間沒到,腳本不會(huì)退出
while True:
hour, minute = get_time()
# 判斷小時(shí)數(shù)和分鐘數(shù)
if hour == HOUR and minute >= MINUTE:
# 一般這里都是一個(gè)字母i,
# 我這里純粹就想循環(huán)五次
# Python里面就可以用 _ 來替代
for _ in range(TIMES):
prompt()
break
if __name__ == '__main__':
main()
-
True
+關(guān)注
關(guān)注
0文章
9瀏覽量
12004 -
窗口
+關(guān)注
關(guān)注
0文章
66瀏覽量
10898 -
函數(shù)
+關(guān)注
關(guān)注
3文章
4346瀏覽量
62977
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
鬧鐘定時(shí)器電路
![<b class='flag-5'>鬧鐘</b><b class='flag-5'>定時(shí)</b>器電路](https://file1.elecfans.com//web2/M00/A6/16/wKgZomUMO6mAXkstAAAXYzQcaks279.jpg)
如何利用Python中的tkinter和MyQR制作個(gè)性二維碼轉(zhuǎn)換器
如何用RTC時(shí)鐘去配置一個(gè)鬧鐘呢
Python是如何使用Tkinter快速創(chuàng)建GUI應(yīng)用程序的
怎樣去設(shè)計(jì)一個(gè)基于單片機(jī)的定時(shí)鬧鐘
【地平線旭日X3派試用體驗(yàn)】40P接口程序開發(fā)體驗(yàn)-基于Python+Tkinter的串口調(diào)試助手
【飛凌RK3568開發(fā)板試用體驗(yàn)】tkinter的第一個(gè)桌面程序
如何用Python制作一個(gè)自動(dòng)發(fā)送程序
使用555定時(shí)器來制作一些樂器
![使用555<b class='flag-5'>定時(shí)</b>器<b class='flag-5'>來</b><b class='flag-5'>制作</b><b class='flag-5'>一</b>些樂器](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
使用Arduino和Python Tkinter進(jìn)行RGB Led控制
![使用Arduino和<b class='flag-5'>Python</b> <b class='flag-5'>Tkinter</b>進(jìn)行RGB Led控制](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何把Python腳本集成到GUI工具包-Tkinter
![如何把<b class='flag-5'>Python</b>腳本集成到GUI工具包-<b class='flag-5'>Tkinter</b>](https://file.elecfans.com/web2/M00/A3/71/pYYBAGRUqXiABt35AAHf9PmpcyI298.png)
Python寫機(jī)器人上位機(jī)
![<b class='flag-5'>Python</b>寫機(jī)器人上位機(jī)](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
如何用Python來實(shí)現(xiàn)文件系統(tǒng)的操作功能
![如<b class='flag-5'>何用</b><b class='flag-5'>Python</b><b class='flag-5'>來</b>實(shí)現(xiàn)文件系統(tǒng)的操作功能](https://file1.elecfans.com/web2/M00/AB/4C/wKgaomU_TFyAdouzAAAGLFfce2g281.jpg)
評(píng)論