亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2018年07月23日 14:30:05   作者:wanlifeipeng  
這篇文章主要介紹了Python Tkinter模塊實現時鐘功能,結合實例形式分析了Tkinter模塊結合time模塊實現的時鐘圖形繪制與計時功能相關操作技巧,需要的朋友可以參考下

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

本機測試效果:

完整代碼:

# coding=utf-8
from Tkinter import *
import _tkinter
import math
import time
from threading import Thread
class 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) # 創(chuàng)建新的線程
    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:
        pass
if __name__ == '__main__':
  root = Tk()
  root.title('chabaoo.cn 時鐘')
  clock = Clock(root, 200, 200, 400, 400, 150)
  clock.drawClockDial()
  clock.run()
  root.mainloop()

待解決的bug:

關閉程序的時候,會出現如下的錯誤:

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設計有所幫助。

相關文章

  • python發(fā)送郵件示例(支持中文郵件標題)

    python發(fā)送郵件示例(支持中文郵件標題)

    python發(fā)送中文郵件示例,支持中文郵件標題和中文郵件內容。支持多附件。根據用戶名推測郵件服務器提供商
    2014-02-02
  • keras 指定程序在某塊卡上訓練實例

    keras 指定程序在某塊卡上訓練實例

    這篇文章主要介紹了keras 指定程序在某塊卡上訓練實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-06-06
  • Python判斷字符串是否為空和null方法實例

    Python判斷字符串是否為空和null方法實例

    這篇文章主要介紹了Python判斷字符串是否為空和null,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python函數返回值實例分析

    Python函數返回值實例分析

    這篇文章主要介紹了Python函數返回值,實例分析了Python中返回一個返回值與多個返回值的方法,需要的朋友可以參考下
    2015-06-06
  • python中類和實例如何綁定屬性與方法示例詳解

    python中類和實例如何綁定屬性與方法示例詳解

    最近在學習python,純粹是自己的興趣愛好,然而并沒有系統(tǒng)地看python編程書籍,覺得上面描述過于繁瑣,在網站找了一些學習的網站,下面這篇文章主要給大家介紹了關于python中類和實例時如何綁定屬性與方法的相關資料,需要的朋友可以參考下。
    2017-08-08
  • python字符串下標與切片及使用方法

    python字符串下標與切片及使用方法

    這篇文章主要介紹了python字符串下標與切片及使用方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-02-02
  • TensorFLow 數學運算的示例代碼

    TensorFLow 數學運算的示例代碼

    這篇文章主要介紹了TensorFLow 數學運算的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • pytorch DataLoader的num_workers參數與設置大小詳解

    pytorch DataLoader的num_workers參數與設置大小詳解

    這篇文章主要介紹了pytorch DataLoader的num_workers參數與設置大小詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 已安裝tensorflow-gpu,但keras無法使用GPU加速的解決

    已安裝tensorflow-gpu,但keras無法使用GPU加速的解決

    今天小編就為大家分享一篇已安裝tensorflow-gpu,但keras無法使用GPU加速的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • 用python畫圣誕樹三種代碼示例介紹

    用python畫圣誕樹三種代碼示例介紹

    大家好,本篇文章主要講的是用python畫圣誕樹三種代碼示例介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12

最新評論