一、Python時鐘製作的思路
製作Python時鐘,我們需要先了解時鐘的基本構成部分,這裡我們推薦使用pyqt5庫來製作一個窗口界面,其中包括一個鐘錶的錶盤以及指針。我們可以通過許多輔助工具來繪製這些圖形元素,但這裡將使用pyqt5來快速創建它們。pyqt5庫是基於Qt的,因此它是跨平台的且兼容性極好。
首先,我們需要引入PyQt5以及相關組件,例如QMainWindow,QCalendarWidget,QTime,以及QtGui和QtCore模塊。我們還需要獲取當前計算機系統的當前時間,並為錶盤和指針指定角度。我們使用Python的嵌套字典和類來管理不同的部件以及其幾何屬性。該字典將位於鐘錶中心的坐標、錶盤的半徑、指針的長度等等事項都包含在內。
接下來,我們會將各個組件放置在正確的位置。我們需要在主窗口中創建一個用於布局的 QGridLayout 對象,然後將其作為主窗口的主布局。我們可以將所有組件添加到必要的布局單元中,依次排列。
<div class="highlight hl-ipython3">
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QWidget
from PyQt5.QtWidgets import QFrame, QCalendarWidget, QLabel
from PyQt5.QtGui import QColor, QPainter, QFont, QPolygon
from PyQt5.QtCore import Qt, QPoint, QTime, QTimer
class DisplayWidget(QWidget):
def __init__(self):
super().__init__()
def set_display(self, display_color, background_color):
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), QColor(background_color))
self.setPalette(p)
self.display_color = display_color
def paintEvent(self, event):
qp = QPainter(self)
font = QFont("Arial", 30)
qp.setFont(font)
qp.setPen(QColor(self.display_color))
qp.drawText(event.rect(), Qt.AlignCenter, self.time_to_text())
二、實現PyQt5顯示時間的功能
我們需要實現可以顯示當前時間的功能。我們使用QTimer來創建一個定時器,每當計時器觸發時,我們都會重新繪製錶盤,然後更新窗口中的標籤。我們定義一個函數time_to_text()來將當前時間轉換為繪製時鐘的文本格式。我們還需將此函數放置在一個帶有日期控件和標籤的小部件中,以配合我們的時鐘錶盤。
<div class="highlight hl-ipython3">
class Clock(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Python Clock")
self.initClock()
def initClock(self):
self.setGeometry(0,0,500,500)
self.grid = QGridLayout(self.centralWidget())
self.grid.setColumnStretch(1, 4)
self.grid.setColumnStretch(3, 4)
self.grid.setRowStretch(2, 4)
self.grid.setRowStretch(4, 4)
self.clock = ClockWidget()
self.grid.addWidget(self.clock, 2, 2, 1, 1)
self.timer = QTimer(self, timeout=self.tick)
self.timer.start(1000)
self.show()
def tick(self):
self.clock.time = QTime.currentTime()
self.clock.update()
class ClockWidget(QWidget):
def __init__(self):
super().__init__()
self.time = QTime(0, 0, 0)
display_widget = DisplayWidget()
display_widget.set_display("#FFA500", "#000000")
calendar_widget = QCalendarWidget()
calendar_widget.setStyleSheet("QWidget {background-color: #000000; color: #FFFFFF;}")
calendar_widget.clicked[QDate].connect(self.showTime)
self.calendar_label = QLabel(self)
self.calendar_label.setFrameShape(QFrame.StyledPanel)
self.calendar_label.setFrameShadow(QFrame.Raised)
self.calendar_label.setAlignment(Qt.AlignCenter)
self.calendar_label.setText('Calendar')
self.grid = QGridLayout(self)
self.grid.addWidget(display_widget, 0, 1, 2, 2)
self.grid.addWidget(calendar_widget, 0, 0, 1, 1)
self.grid.addWidget(self.calendar_label, 1, 0, 1, 1)
def time_to_text(self):
return self.time.toString('hh:mm:ss')
def showTime(self, date):
self.time = QTime.currentTime()
self.calendar_label.setText(date.toString() + '\n' + self.time.toString('hh:mm:ss'))
def paintEvent(self, event):
qp = QPainter(self)
d = min(self.width(), self.height())
r = d / 2
flower_polygon = QPolygon([
QPoint(2, 2), QPoint(3, 0), QPoint(5, 0), QPoint(6, 2),
QPoint(6, 3), QPoint(5, 5), QPoint(3, 5), QPoint(2, 3)
])
flower_polygon = flower_polygon.scaled(r, r, Qt.KeepAspectRatio)
qp.translate(self.width() / 2, self.height() / 2)
qp.scale(r, r)
qp.setPen(Qt.NoPen)
qp.setBrush(QColor("#FFA500"))
qp.drawEllipse(-1, -1, 2, 2)
qp.setBrush(QColor("#FFD700"))
qp.drawPolygon(flower_polygon)
for i in range(12):
angle = i * 30
qp.rotate(angle)
qp.drawLine(0.8, 0, 1, 0)
qp.rotate(-angle)
# second hand
s = self.time.second()
qp.setPen(QColor("#FF0000"))
qp.setBrush(QColor("#FF0000"))
qp.rotate(6.0 * s)
qp.drawLine(0, 0, 0.8, 0)
qp.drawEllipse(-0.05, -0.05, 0.10, 0.10)
# minute hand
m = self.time.minute()
qp.setPen(QColor("#0000FF"))
qp.setBrush(QColor("#0000FF"))
qp.rotate(6.0 * m)
qp.drawLine(0, 0, 0.7, 0)
qp.drawEllipse(-0.05, -0.05, 0.10, 0.10)
# hour hand
h = self.time.hour() % 12
qp.setPen(QColor("#00FF00"))
qp.setBrush(QColor("#00FF00"))
qp.rotate(30.0 * h + 0.5 * m)
qp.drawLine(0, 0, 0.5, 0)
qp.drawEllipse(-0.05, -0.05, 0.10, 0.10)
三、結論與參考
本文中,我們介紹了如何使用Python的PyQt5庫創建一個類似於窗口表中的時鐘界面。我們了解了製作時鐘的基本構成、實現具體的功能及相應的代碼。PyQt5是非常強大和高效的。特別是,它可以嵌入不同的計時器和日期對象,以便更好地實現時鐘所需的特定功能。
如果您需要更多的信息或其他格式上的變化,可以參考PyQt5文檔,獲取關於包括GUI、數據源等其他方面的信息。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/198375.html