浮动时钟,多地时区
app store的都要钱,于是。。。。我们让chatgpt来实现一个吧:
数字:
代码:
import sys
import datetime
import pytz
from 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, QBrush
class 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 name
city_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 needed
self.scene.addItem(city_text)
# Add digital time
self.digital_time = QGraphicsTextItem()
self.digital_time.setFont(QFont("Arial", 30, QFont.Bold))
self.digital_time.setDefaultTextColor(QColor(255, 215, 0)) # Golden color
self.digital_time.setPos(10, 50) # Adjust position as needed
self.scene.addItem(self.digital_time)
# Create gradient background
gradient = QLinearGradient(0, 0, 0, 600)
gradient.setColorAt(0.0, QColor(0, 0, 0)) # Silver color
gradient.setColorAt(1.0, QColor(255, 255, 255)) # White color
self.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 time
self.digital_time.setPlainText(time)
super().update()
class ClockApp(QMainWindow):
def __init__(self):
super().__init__()
# Create the widget container and layout
self.widget = QWidget()
self.layout = QHBoxLayout()
# Create the clock widgets
self.clock1 = ClockWidget('Asia/Shanghai', 'Beijing', QColor(255, 255, 255)) # Red color for location
self.clock2 = ClockWidget('Europe/Paris', 'Paris', QColor(255, 255, 255)) # Blue color for location
# Add clocks to the layout
self.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 button
self.exit_button = QPushButton('X') # Add 'X' as the button text
self.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 button
self.exit_button.clicked.connect(QCoreApplication.instance().quit) # Connect button click to exit action
# Add Exit button to the layout
self.layout.addWidget(self.exit_button)
# Set layout and window properties
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
self.setGeometry(300, 300, 500, 100) # Adjust window size as needed
self.setWindowFlags(Qt.FramelessWindowHint) # Remove window bar
# For dragging the window
self.m_mouse_down = False
self.m_last_pos = None
# Mouse press event
def mousePressEvent(self, event):
self.m_mouse_down = True
self.m_last_pos = event.pos()
# Mouse move event
def mouseMoveEvent(self, event):
if self.m_mouse_down:
self.move(self.pos() + (event.pos() - self.m_last_pos))
# Mouse release event
def mouseReleaseEvent(self, event):
self.m_mouse_down = False
if __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, QLinearGradient
class 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 widths
self.hour_hand = QGraphicsLineItem(0, 0, 0, -60)
self.hour_hand.setPen(QPen(QColor(255, 0, 0), 5)) # Red color, width 5
self.minute_hand = QGraphicsLineItem(0, 0, 0, -80)
self.minute_hand.setPen(QPen(QColor(0, 255, 0), 4)) # Green color, width 4
self.second_hand = QGraphicsLineItem(0, 0, 0, -90)
self.second_hand.setPen(QPen(QColor(0, 0, 255), 3)) # Blue color, width 3
for hand in [self.hour_hand, self.minute_hand, self.second_hand]:
hand.setPos(150, 150)
self.scene.addItem(hand)
# Add clock ticks
for 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 positions
font = 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 name
city_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 needed
self.scene.addItem(city_text)
# Add digital time
self.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 needed
self.scene.addItem(self.digital_time)
# Create gradient background
gradient = QLinearGradient(0, 0, 0, 600)
gradient.setColorAt(0.0, QColor(192, 192, 192)) # Silver color
gradient.setColorAt(1.0, QColor(255, 255, 255)) # White color
self.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 time
self.digital_time.setPlainText(time.toString())
super().update()
class ClockApp(QMainWindow):
def __init__(self):
super().__init__()
# Create the widget container and layout
self.widget = QWidget()
self.layout = QHBoxLayout()
# Create the clock widgets
self.clock1 = ClockWidget('Asia/Shanghai', QColor(255, 0, 0))
self.clock2 = ClockWidget('Europe/Paris', QColor(0, 0, 255))
# Add clocks to the layout
self.layout.addWidget(self.clock1)
self.layout.addWidget(self.clock2)
# Set layout and window properties
self.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 window
self.m_mouse_down = False
self.m_last_pos = QPoint()
# Mouse press event
def mousePressEvent(self, event):
self.m_mouse_down = True
self.m_last_pos = event.pos()
# Mouse move event
def mouseMoveEvent(self, event):
if self.m_mouse_down:
self.move(self.pos() + (event.pos() - self.m_last_pos))
# Mouse release event
def mouseReleaseEvent(self, event):
self.m_mouse_down = False
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = ClockApp()
ex.show()
sys.exit(app.exec_())
版权声明:本文为bagba原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。