2017年10月10日 星期二
Tkinter DateTime
"""利用tkinter和datetime模組來寫時鐘程式
datetime模組主要用來獲得當前時區的時間。
now = datetime.datetime.now能夠獲得當前時區的時間。
然後利用now.year,now.minute,now.second就分別能得到int類型的數值。
主要難點是利用self.canvas.after(200),200代表200毫秒,
也就是等待200毫秒後執行後續代碼。
結合while True迴圈和self.canvas.update(),
該代碼會以200毫秒為週期迴圈更新時間。
另外,還要考慮時針和分針轉一圈不僅僅只有60個位置,
可以細化到更為精細的移動。"""
import datetime
import math
from tkinter import *
class CurrentTime:
def __init__(self):
window = Tk()
window.title("Current Time")
self.canvas = Canvas(window, width = 200, height = 200, bg = "white")
self.canvas.pack()
self.canvas.create_text(100,10, text = "CLOCK", font = "bond")
self.canvas.create_text(100,30, text = "12", font = "bond")
self.canvas.create_text(135,40, text = "1", font = "bond")
self.canvas.create_text(160,65, text = "2", font = "bond")
self.canvas.create_text(170,100, text = "3", font = "bond")
self.canvas.create_text(160,135, text = "4", font = "bond")
self.canvas.create_text(135,160, text = "5", font = "bond")
self.canvas.create_text(100,170, text = "6", font = "bond")
self.canvas.create_text(65,160, text = "7", font = "bond")
self.canvas.create_text(40,135, text = "8", font = "bond")
self.canvas.create_text(30,100, text = "9", font = "bond")
self.canvas.create_text(40,65, text = "10", font = "bond")
self.canvas.create_text(65,40, text = "11", font = "bond")
self.canvas.create_oval(20, 20, 180, 180, width = 2)
self.showTime()
window.mainloop()
def showTime(self):
while True:
self.canvas.delete("time", "line")
now = datetime.datetime.now()
self.hour = now.hour
self.minute = now.minute
self.second = now.second
self.canvas.create_text(100,190, text = str(self.hour) + ":" + str(self.minute) \
+ ":" + str(self.second), tags = "time")
self.showLines()
self.canvas.after(200)
self.canvas.update()
def showLines(self):
if self.hour > 12 :
hour = self.hour - 12
second = self.second
minute = self.minute + second/60
hour += minute/60
theta1 = - hour / 12 * 2 * math.pi + 0.5 * math.pi
theta2 = - minute / 60 * 2 * math.pi + 0.5 * math.pi
theta3 = - second / 60 * 2 * math.pi + 0.5 * math.pi
tanTheta1 = math.tan(theta1)
tanTheta2 = math.tan(theta2)
tanTheta3 = math.tan(theta3)
r1 = 40
r2 = 55
r3 = 65
x1 = 100 + r1 * math.cos(theta1)
x2 = 100 + r2 * math.cos(theta2)
x3 = 100 + r3 * math.cos(theta3)
y1 = tanTheta1 * x1 + 100 * (1 - tanTheta1)
y2 = tanTheta2 * x2 + 100 * (1 - tanTheta2)
y3 = tanTheta3 * x3 + 100 * (1 - tanTheta3)
y1 = 200 - y1
y2 = 200 - y2
y3 = 200 - y3
if y1 > r1 + 100:
y1 = -r1 + 100
if y1 < -r1 + 100:
y1 = r1 + 100
if y2 > r2 + 100:
y2 = -r2 + 100
if y2 < -r2 + 100:
y2 = r2 + 100
if y3 > r3 + 100:
y3 = -r3 + 100
if y3 < -r3 + 100:
y3 = r3 + 100
self.canvas.create_line(x1, y1, 100, 100, arrow = "first", width = 2, tags = "line")
self.canvas.create_line(x2, y2, 100, 100, arrow = "first", width = 2, tags = "line")
self.canvas.create_line(x3, y3, 100, 100, fill = "red", tags = "line")
CurrentTime()
訂閱:
張貼留言 (Atom)
113 學年度第 1 學期 RFID應用課程 Arduino程式
113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言