浮动时钟,多地时区
app store的都要钱,于是。。。。我们让chatgpt来实现一个吧:
数字:
代码:
import sys
import datetime
import pytzfrom PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsTextItem, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import QTimer, Qt, QCoreApplication
from PyQt5.QtGui import QColor, QFont, QIcon, QLinearGradient, QBrushclass ClockWidget(QGraphicsView):def __init__(self, timezone, location, color):super().__init__()self.scene = QGraphicsScene(self)self.setScene(self.scene)self.setGeometry(0, 0, 200, 100) # Adjust width and height as needed# Add city namecity_font = QFont("Arial", 30, QFont.Bold)city_text = QGraphicsTextItem(location)city_text.setFont(city_font)city_text.setDefaultTextColor(color)city_text.setPos(10, 10) # Adjust position as neededself.scene.addItem(city_text)# Add digital timeself.digital_time = QGraphicsTextItem()self.digital_time.setFont(QFont("Arial", 30, QFont.Bold))self.digital_time.setDefaultTextColor(QColor(255, 215, 0)) # Golden colorself.digital_time.setPos(10, 50) # Adjust position as neededself.scene.addItem(self.digital_time)# Create gradient backgroundgradient = QLinearGradient(0, 0, 0, 600)gradient.setColorAt(0.0, QColor(0, 0, 0)) # Silver colorgradient.setColorAt(1.0, QColor(255, 255, 255)) # White colorself.setBackgroundBrush(QBrush(gradient))self.timer = QTimer(self)self.timer.timeout.connect(self.update)self.timezone = pytz.timezone(timezone)self.timer.start(1000)def update(self):now = datetime.datetime.now(self.timezone)time = now.strftime("%H:%M:%S") # Format time as hour:min:seconds# Update digital timeself.digital_time.setPlainText(time)super().update()class ClockApp(QMainWindow):def __init__(self):super().__init__()# Create the widget container and layoutself.widget = QWidget()self.layout = QHBoxLayout()# Create the clock widgetsself.clock1 = ClockWidget('Asia/Shanghai', 'Beijing', QColor(255, 255, 255)) # Red color for locationself.clock2 = ClockWidget('Europe/Paris', 'Paris', QColor(255, 255, 255)) # Blue color for location# Add clocks to the layoutself.layout.addWidget(self.clock1)self.layout.addWidget(self.clock2)# Create Exit button# self.exit_button = QPushButton()# self.exit_button.setIcon(QIcon('exit.png')) # Path to the image file for the button# self.exit_button.setStyleSheet("background-color: red")# self.exit_button.clicked.connect(QCoreApplication.instance().quit) # Connect button click to exit action# Create Exit button# Create Exit buttonself.exit_button = QPushButton('X') # Add 'X' as the button textself.exit_button.setStyleSheet("QPushButton {background-color: gray; color: white; font-weight: bold; font-size: 18px; border-radius: 15px; width: 30px; height: 30px;}""QPushButton:pressed {background-color: darkred;}")self.exit_button.setFixedSize(30, 30) # Fix the size of the buttonself.exit_button.clicked.connect(QCoreApplication.instance().quit) # Connect button click to exit action# Add Exit button to the layoutself.layout.addWidget(self.exit_button)# Set layout and window propertiesself.widget.setLayout(self.layout)self.setCentralWidget(self.widget)self.setGeometry(300, 300, 500, 100) # Adjust window size as neededself.setWindowFlags(Qt.FramelessWindowHint) # Remove window bar# For dragging the windowself.m_mouse_down = Falseself.m_last_pos = None# Mouse press eventdef mousePressEvent(self, event):self.m_mouse_down = Trueself.m_last_pos = event.pos()# Mouse move eventdef mouseMoveEvent(self, event):if self.m_mouse_down:self.move(self.pos() + (event.pos() - self.m_last_pos))# Mouse release eventdef mouseReleaseEvent(self, event):self.m_mouse_down = Falseif __name__ == '__main__':app = QApplication(sys.argv)ex = ClockApp()ex.show()sys.exit(app.exec_())
模拟加数字效果:
代码:
import sys
import datetime
import pytz
from PyQt5.QtWidgets import QApplication, QMainWindow, QGraphicsView, QGraphicsScene, QGraphicsLineItem, QGraphicsTextItem, QWidget, QHBoxLayout
from PyQt5.QtCore import QTimer, QTime, Qt, QPointF, QPoint
from PyQt5.QtGui import QColor, QTransform, QPen, QFont, QBrush, QLinearGradientclass ClockWidget(QGraphicsView):def __init__(self, timezone, color):super().__init__()self.scene = QGraphicsScene(self)self.setScene(self.scene)self.setGeometry(0, 0, 300, 300)# Define the clock hands with their colors and widthsself.hour_hand = QGraphicsLineItem(0, 0, 0, -60)self.hour_hand.setPen(QPen(QColor(255, 0, 0), 5)) # Red color, width 5self.minute_hand = QGraphicsLineItem(0, 0, 0, -80)self.minute_hand.setPen(QPen(QColor(0, 255, 0), 4)) # Green color, width 4self.second_hand = QGraphicsLineItem(0, 0, 0, -90)self.second_hand.setPen(QPen(QColor(0, 0, 255), 3)) # Blue color, width 3for hand in [self.hour_hand, self.minute_hand, self.second_hand]:hand.setPos(150, 150)self.scene.addItem(hand)# Add clock ticksfor i in range(12):item = QGraphicsLineItem(0, 0, 0, -100)item.setPos(150, 150)item.setPen(QPen(QColor(255, 215, 0), 2))item.setTransform(QTransform().rotate(i * 30))self.scene.addItem(item)# Add numbers at top, right, bottom, left positionsfont = QFont("Arial", 16)numbers = {'12': QPointF(150, 50), '3': QPointF(250, 150), '6': QPointF(150, 250), '9': QPointF(50, 150)}for number, position in numbers.items():text_item = QGraphicsTextItem(number)text_item.setFont(font)text_item.setPos(position)self.scene.addItem(text_item)# Add city namecity_font = QFont("Arial", 30)city_text = QGraphicsTextItem(timezone)city_text.setFont(city_font)city_text.setDefaultTextColor(color)city_text.setPos(50, 10) # Adjust position as neededself.scene.addItem(city_text)# Add digital timeself.digital_time = QGraphicsTextItem()self.digital_time.setFont(QFont("Arial", 30, QFont.Bold))self.digital_time.setDefaultTextColor(Qt.black)self.digital_time.setPos(60, 260) # Adjust position as neededself.scene.addItem(self.digital_time)# Create gradient backgroundgradient = QLinearGradient(0, 0, 0, 600)gradient.setColorAt(0.0, QColor(192, 192, 192)) # Silver colorgradient.setColorAt(1.0, QColor(255, 255, 255)) # White colorself.setBackgroundBrush(QBrush(gradient))self.timer = QTimer(self)self.timer.timeout.connect(self.update)self.timezone = pytz.timezone(timezone)self.timer.start(1000)def update(self):now = datetime.datetime.now(self.timezone)time = QTime(now.hour, now.minute, now.second)self.hour_hand.setTransform(QTransform().rotate(30.0 * (time.hour() + time.minute() / 60.0)))self.minute_hand.setTransform(QTransform().rotate(6.0 * (time.minute() + time.second() / 60.0)))self.second_hand.setTransform(QTransform().rotate(6.0 * time.second()))# Update digital timeself.digital_time.setPlainText(time.toString())super().update()class ClockApp(QMainWindow):def __init__(self):super().__init__()# Create the widget container and layoutself.widget = QWidget()self.layout = QHBoxLayout()# Create the clock widgetsself.clock1 = ClockWidget('Asia/Shanghai', QColor(255, 0, 0))self.clock2 = ClockWidget('Europe/Paris', QColor(0, 0, 255))# Add clocks to the layoutself.layout.addWidget(self.clock1)self.layout.addWidget(self.clock2)# Set layout and window propertiesself.widget.setLayout(self.layout)self.setCentralWidget(self.widget)self.setGeometry(300, 300, 600, 350) # Adjusts window size. Format: (x_position, y_position, width, height)self.setWindowFlags(Qt.FramelessWindowHint) # Remove window bar# For dragging the windowself.m_mouse_down = Falseself.m_last_pos = QPoint()# Mouse press eventdef mousePressEvent(self, event):self.m_mouse_down = Trueself.m_last_pos = event.pos()# Mouse move eventdef mouseMoveEvent(self, event):if self.m_mouse_down:self.move(self.pos() + (event.pos() - self.m_last_pos))# Mouse release eventdef mouseReleaseEvent(self, event):self.m_mouse_down = Falseif __name__ == '__main__':app = QApplication(sys.argv)ex = ClockApp()ex.show()sys.exit(app.exec_())