使用pyqt5編寫一個七彩時鐘
- 效果
- 代碼解析
- 定義 RainbowClockWindow 類
- 初始化用戶界面
- 顯示時間方法
- 完整代碼
在這篇博客中,我們將使用 PyQt5 創建一個簡單的七彩數字時鐘。
效果
代碼解析
定義 RainbowClockWindow 類
class RainbowClockWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle('Rainbow Digital Clock')self.setGeometry(100, 100, 400, 200)self.initUI()
初始化用戶界面
def initUI(self):layout = QVBoxLayout()self.time_layout = QHBoxLayout()self.time_layout.setSpacing(0) # 設置標簽之間的間距為0self.hour_label = QLabel(self)self.hour_label.setAlignment(Qt.AlignCenter)self.hour_label.setStyleSheet("font-size: 48px;")self.colon1_label = QLabel(self)self.colon1_label.setAlignment(Qt.AlignCenter)self.colon1_label.setStyleSheet("font-size: 48px;")self.colon1_label.setText(":")self.minute_label = QLabel(self)self.minute_label.setAlignment(Qt.AlignCenter)self.minute_label.setStyleSheet("font-size: 48px;")self.colon2_label = QLabel(self)self.colon2_label.setAlignment(Qt.AlignCenter)self.colon2_label.setStyleSheet("font-size: 48px;")self.colon2_label.setText(":")self.second_label = QLabel(self)self.second_label.setAlignment(Qt.AlignCenter)self.second_label.setStyleSheet("font-size: 48px;")self.time_layout.addWidget(self.hour_label)self.time_layout.addWidget(self.colon1_label)self.time_layout.addWidget(self.minute_label)self.time_layout.addWidget(self.colon2_label)self.time_layout.addWidget(self.second_label)layout.addLayout(self.time_layout)layout.setAlignment(Qt.AlignCenter) # 居中對齊container = QWidget()container.setLayout(layout)self.setCentralWidget(container)timer = QTimer(self)timer.timeout.connect(self.showTime)timer.start(1000)self.showTime()
- 創建一個垂直布局 QVBoxLayout 和一個水平布局 QHBoxLayout,并設置水平布局的標簽間距為0。
- 創建五個標簽:hour_label、colon1_label、minute_label、colon2_label 和
second_label,并設置標簽的對齊方式和樣式,使其在窗口中居中并且字體大小為 48 像素。 - 將五個標簽添加到水平布局中。
- 將水平布局添加到垂直布局中,并設置垂直布局居中對齊。
- 創建一個容器 QWidget,將布局設置為該容器的布局,并將容器設置為主窗口的中央控件。
- 創建一個 QTimer 對象,每秒觸發一次 timeout 事件,連接到 showTime 方法。
- 調用 showTime 方法顯示當前時間。
顯示時間方法
def showTime(self):current_time = QTime.currentTime()hour = current_time.toString('hh')minute = current_time.toString('mm')second = current_time.toString('ss')# Generate random colors for hour, minute, and secondhour_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))minute_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))second_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))self.hour_label.setText(hour)self.hour_label.setStyleSheet(f"font-size: 48px; color: {hour_color.name()};")self.minute_label.setText(minute)self.minute_label.setStyleSheet(f"font-size: 48px; color: {minute_color.name()};")self.second_label.setText(second)self.second_label.setStyleSheet(f"font-size: 48px; color: {second_color.name()};")# Colon colorsself.colon1_label.setStyleSheet(f"font-size: 48px; color: #000000;")self.colon2_label.setStyleSheet(f"font-size: 48px; color: #000000;")
- showTime 方法獲取當前時間并將其格式化為小時、分鐘和秒。
- 為小時、分鐘和秒生成隨機顏色,并將這些顏色應用到相應的標簽上。
- 將兩個冒號標簽的顏色固定為黑色。
完整代碼
import sys
import random
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QHBoxLayout, QVBoxLayout, QWidget
from PyQt5.QtCore import QTimer, QTime, Qt
from PyQt5.QtGui import QColorclass RainbowClockWindow(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle('Rainbow Digital Clock')self.setGeometry(100, 100, 400, 200)self.initUI()def initUI(self):layout = QVBoxLayout()self.time_layout = QHBoxLayout()self.time_layout.setSpacing(0) # 設置標簽之間的間距為0self.hour_label = QLabel(self)self.hour_label.setAlignment(Qt.AlignCenter)self.hour_label.setStyleSheet("font-size: 48px;")self.colon1_label = QLabel(self)self.colon1_label.setAlignment(Qt.AlignCenter)self.colon1_label.setStyleSheet("font-size: 48px;")self.colon1_label.setText(":")self.minute_label = QLabel(self)self.minute_label.setAlignment(Qt.AlignCenter)self.minute_label.setStyleSheet("font-size: 48px;")self.colon2_label = QLabel(self)self.colon2_label.setAlignment(Qt.AlignCenter)self.colon2_label.setStyleSheet("font-size: 48px;")self.colon2_label.setText(":")self.second_label = QLabel(self)self.second_label.setAlignment(Qt.AlignCenter)self.second_label.setStyleSheet("font-size: 48px;")self.time_layout.addWidget(self.hour_label)self.time_layout.addWidget(self.colon1_label)self.time_layout.addWidget(self.minute_label)self.time_layout.addWidget(self.colon2_label)self.time_layout.addWidget(self.second_label)layout.addLayout(self.time_layout)layout.setAlignment(Qt.AlignCenter) # 居中對齊container = QWidget()container.setLayout(layout)self.setCentralWidget(container)timer = QTimer(self)timer.timeout.connect(self.showTime)timer.start(1000)self.showTime()def showTime(self):current_time = QTime.currentTime()hour = current_time.toString('hh')minute = current_time.toString('mm')second = current_time.toString('ss')# Generate random colors for hour, minute, and secondhour_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))minute_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))second_color = QColor(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))self.hour_label.setText(hour)self.hour_label.setStyleSheet(f"font-size: 48px; color: {hour_color.name()};")self.minute_label.setText(minute)self.minute_label.setStyleSheet(f"font-size: 48px; color: {minute_color.name()};")self.second_label.setText(second)self.second_label.setStyleSheet(f"font-size: 48px; color: {second_color.name()};")# Colon colorsself.colon1_label.setStyleSheet(f"font-size: 48px; color: #000000;")self.colon2_label.setStyleSheet(f"font-size: 48px; color: #000000;")if __name__ == "__main__":app = QApplication(sys.argv)window = RainbowClockWindow()window.show()sys.exit(app.exec_())