国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python Tkinter模塊實現時鐘功能應用示例

2020-02-15 22:30:23
字體:
來源:轉載
供稿:網友

本文實例講述了Python Tkinter模塊實現時鐘功能。分享給大家供大家參考,具體如下:

本機測試效果:

完整代碼:

# coding=utf-8from Tkinter import *import _tkinterimport mathimport timefrom threading import Threadclass Clock:  def __init__(self, master, x, y, width, height, radius):    '''    :param master: 父窗口    :param x: 時鐘中心點的x坐標    :param y: 時鐘中心點的y坐標    :param width: 畫布的寬度    :param height: 畫布的高度    :param radius: 時鐘鐘盤的半徑    '''    self.centerX = x    self.centerY = y    self.radius = radius    self.canvas = Canvas(master, width=width, height=height) # 畫布    self.canvas.pack()    self.canvas.create_oval(      x - radius,      y - radius,      x + radius,      y + radius) # 畫鐘框    self.id_lists = []    self.hourHandRadius = self.radius * 1.0 / 4  # 指針長度    self.minHandRadius = self.radius * 2.0 / 3  # 分針長度    self.secHandRadius = self.radius * 4.0 / 5  # 秒針長度    self.timeVar = StringVar()    # self.timeVar.set('')    self.timeLabel = Label(self.canvas.master, textvariable=self.timeVar)    self.timeLabel.pack(side=BOTTOM)    #self.canvas.master.protocol('WM_DELETE_WINDOW', self.canvas.master.destroy)  def __del__(self):    self._deleteItems(self.id_lists)  # 繪制時鐘鐘盤  def drawClockDial(self):    # 繪制鐘盤上的數字1-12    r = self.radius - 15    for i in range(1, 13):      rad = 2 * math.pi / 12 * i      x = self.centerX + math.sin(rad) * r      y = self.centerY - math.cos(rad) * r      id = self.canvas.create_text(x, y, text=str(i))      self.id_lists.append(id)    # 繪制鐘盤上的刻度    r1 = self.radius - 5    r2 = self.radius    for i in range(1, 61):      rad = 2 * math.pi / 60 * i      x1, y1 = self._getPosByRadAndRadius(rad, r1)      x2, y2 = self._getPosByRadAndRadius(rad, r2)      id = self.canvas.create_line(x1, y1, x2, y2)      self.id_lists.append(id)  # 顯示時間  def showTime(self, tm):    hour = tm.tm_hour % 12    min = tm.tm_min    sec = tm.tm_sec    sec_rad = 2 * math.pi / 60 * sec    min_rad = 2 * math.pi / 60 * (min + sec / 60.0)    hour_rad = 2 * math.pi / 12 * (hour + min / 60.0)    timeStr = '當前時間: %d-%02d-%02d %02d:%02d:%02d' % (      tm.tm_year, tm.tm_mon, tm.tm_mday, hour, min, sec)    self.timeVar.set(timeStr)    hour_id = self._drawLine(hour_rad, self.hourHandRadius, 6)    min_id = self._drawLine(min_rad, self.minHandRadius, 4)    sec_id = self._drawLine(sec_rad, self.secHandRadius, 3)    return (hour_id, min_id, sec_id)  def run(self):    def _run():      while True:        tm = time.localtime()        id_lists = self.showTime(tm)        self.canvas.master.update()        time.sleep(1)        self._deleteItems(id_lists)    thrd = Thread(target=_run) # 創建新的線程    thrd.run() # 啟動線程  def _drawLine(self, rad, radius, width):    x, y = self._getPosByRadAndRadius(rad, radius)    id = self.canvas.create_line(      self.centerX, self.centerY, x, y, width=width)    return id  def _getPosByRadAndRadius(self, rad, radius):    x = self.centerX + radius * math.sin(rad)    y = self.centerY - radius * math.cos(rad)    return (x, y)  def _deleteItems(self, id_lists):    for id in id_lists:      try:        self.canvas.delete(id)      except BaseException:        passif __name__ == '__main__':  root = Tk()  root.title('www.jb51.net 時鐘')  clock = Clock(root, 200, 200, 400, 400, 150)  clock.drawClockDial()  clock.run()  root.mainloop()            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普格县| 句容市| 五家渠市| 桂平市| 丹东市| 泰来县| 礼泉县| 溆浦县| 仙居县| 兴业县| 萨迦县| 乾安县| 京山县| 昌黎县| 沭阳县| 哈尔滨市| 徐闻县| 睢宁县| 永仁县| 辛集市| 秦皇岛市| 祥云县| 卓资县| 务川| 金塔县| 宜昌市| 新兴县| 永吉县| 台北市| 鸡西市| 邳州市| 田东县| 日土县| 吉林省| 清流县| 五常市| 峨眉山市| 宜宾县| 鹤峰县| 正蓝旗| 奉贤区|