打造你的“时间小偷”:一个PyQt5计时器

打造你的“时间小偷”:一个PyQt5计时器的搞笑冒险

在这个快节奏的世界里,每个人都在争分夺秒,但有时候,我们也需要一个“时间小偷”——一个能让我们在忙碌中偷得浮生半日闲的PyQt5计时器。别担心,这个“小偷”不会让你破产,反而会让你笑得前仰后合,同时学会如何用代码“偷”时间。

序幕:与时间的“猫鼠游戏”

想象一下,你是一个程序员界的“侦探”,你的任务是设计一个超级智能(或者说,超级搞笑)的计时器,它不仅要能精准地“偷”走时间,还要能在你无聊的时候给你讲个冷笑话(虽然它可能只会显示“已过去1小时,你的咖啡凉了”这样的“笑话”)。

第一幕:界面设计——从“简陋”到“简陋但搞笑”

首先,我们得有个“脸面”,对吧?于是,你打开PyQt5的魔法箱,开始摆弄那些神奇的控件。你创建了一个QMainWindow,它就像你的计时器大厦的“门面”。然后,你添加了一个QComboBox作为语言选择器,告诉它:“嘿,兄弟,你不仅要会英文,还得会中文,不然我怎么跟我的中国粉丝炫耀?”

接着,你放了一个大大的QPushButton,上面写着“开始/停止”,就像是一个随时准备按下的“时间暂停键”。但是,你心里知道,一旦按下,时间就会像脱缰的野马一样狂奔而去,再也不回头。

最后,你加了一个QLabel来显示时间,它就像是一个忠实的“时间播报员”,但有时候,你也想让它说点别的,比如:“你已经盯着这个屏幕看了5分钟,该去喝水了!”

第二幕:功能实现——与时间的“智斗”

现在,是时候让QTimer这个“时间小偷”上场了。你设置了一个定时器,每秒钟更新一次时间显示。你心想:“这下好了,我可以看着时间一点点流逝,然后假装自己很忙碌。”

但是,你很快就发现,这个“小偷”有点调皮。它不仅仅偷走了时间,还偷走了你的注意力。每次你看到时间跳动,都会忍不住想:“哎呀,又过去一秒了,我得赶紧做点什么!”

于是,你开始和“时间小偷”玩起了“猫鼠游戏”。你试图抓住它,但每次都只是看着它从你的指尖溜走。不过,这也没关系,毕竟,能和一个这么有趣的“小偷”玩耍,也是一种乐趣嘛!

第三幕:语言切换——从“一本正经”到“胡说八道”

为了让你的计时器更加国际化(或者说,更加搞笑),你添加了一个语言切换功能。现在,用户可以在英文和中文之间自由切换。你想象着用户一脸迷茫地看着屏幕上的中文提示:“你已经成功浪费了30分钟,快去晒太阳吧!”然后哈哈大笑。

当然,你也知道,这个功能可能会让一些用户感到困惑。比如,他们可能会问:“为什么这个计时器会突然跟我说中文?难道它是个外星人?”但这就是你的目的啊——让你的计时器成为一个充满惊喜和欢笑的“时间小偷”。

第四幕:“时间小偷”在哪里?

文章的题目都有py了,那他肯定是用
Python
编写的,他在GitHub(点击观看)https://github.com/wd6231code/cool-clock里,不过,我已经把他拉过来了,“时间小偷”即将要出场了,3,2,1,入场!!!

import sys  
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget, QApplication, QMessageBox,QComboBox  
from PyQt5.QtCore import QTimer, QTime  
from PyQt5.QtGui import QFont    

# The default is Chinese
index = 1
# Make SQL file

class TimerWindow(QMainWindow):  
    def __init__(self):  
        super().__init__()  
  
        self.initUI()  
  
    def initUI(self):  
        self.setWindowTitle('PyQt5 Timer')  
        self.setGeometry(100, 100, 400, 300)  
  
        central_widget = QWidget(self)  
        self.setCentralWidget(central_widget)  
  
        layout = QVBoxLayout(central_widget)  
  
        # Language selection  
        self.language_combo = QComboBox(self)  
        self.language_combo.addItems(['English', '中文'])  
        self.language_combo.currentIndexChanged.connect(self.change_language)  
        layout.addWidget(self.language_combo)  
  
        # Timer controls  
        self.start_stop_button = QPushButton(self.get_translated_text('Start/Stop Timer'), self)  
        self.start_stop_button.clicked.connect(self.toggle_timer)  
        layout.addWidget(self.start_stop_button)  
  
        # Fullscreen controls (not fully implemented for simplicity)  
        self.full_screen_button = QPushButton(self.get_translated_text('Go Fullscreen'), self)  
        self.full_screen_button.clicked.connect(self.toggle_fullscreen)  # Placeholder for fullscreen functionality  
        layout.addWidget(self.full_screen_button)  
  
        # Timer display  
        self.time_label = QLabel('00:00:00', self)  
        layout.addWidget(self.time_label)  
  
        # Setup timers  
        self.timer = QTimer(self)  
        self.timer.timeout.connect(self.update_time)  
  
        # Initial timer state  
        self.timer_running = False  
  
        # Set a font that supports both English and Chinese  
        font = QFont()  
        font.setFamily('Microsoft YaHei')  # Or any other font that supports Chinese  
        font.setPointSize(12)  
        self.setFont(font)  # Set the font for the main window, which will propagate to child widgets  
  
    def toggle_timer(self):  
        if self.timer_running:  
            self.timer.stop()  
            self.start_stop_button.setText(self.get_translated_text('Start Timer'))  
        else:  
            self.timer.start(1000)  # Update time every 1000 ms (1 second)  
            self.start_stop_button.setText(self.get_translated_text('Stop Timer'))  
        self.timer_running = not self.timer_running  
  
    def update_time(self):  
        # Placeholder for actual time updating logic  
        # Here we just display a static time for demonstration  
        self.time_label.setText('00:01:00')  # In a real application, you would update this based on elapsed time  
  
    def toggle_fullscreen(self):  
        # Placeholder for fullscreen functionality  
        # In a real application, you would implement this to toggle fullscreen mode  
        pass  
  
    def get_translated_text(self, text):  
        # Simple translation function based on the current language selection  
        # In a real application, you might use a more sophisticated translation system  
        index = self.language_combo.currentIndex()  
        

        
     
# Main function to run the application  
if __name__ == '__main__':  
    app = QApplication(sys.argv)  
    ex = TimerWindow()  
    ex.show()
    sys.exit(app.exec_())
     

尾声:与“时间小偷”的和谐共处

最后,当你完成了这个搞笑又实用的PyQt5计时器时,你深深地吸了一口气。你意识到,虽然你无法真正地“偷”走时间,但你可以通过编程的方式,让时间变得更加有趣和有意义。

所以,下次当你看到那个“开始/停止”按钮时,不妨微笑着按下它。然后,看着时间在你的屏幕上跳跃、舞动——就像是一个永远不会疲倦的舞者。而你,就是那个能够控制这个舞者节奏的“导演”。

现在,去享受你的“时间小偷”吧!记得要笑得开心哦!
在这个

附件:原理

操控
传输
主循环
GUI
等待按钮被按
执行相应动作
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值