用户:氯化氢不是氯代烃查看:0 回复:1 评论:0 创建时间:2022-03-01T19:41:03
【作品展示】

【作品介绍】
它可以定时提醒用户(弹出图片,播放音乐),让用户注意不要连续用电脑太久。它附属的小猫图标,右键单击后会显示功能菜单。
【作品源代码】
import sys
import random
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtMultimedia import *
from ui_reminder import Ui_Reminder
import time
class Reminder(QMainWindow, Ui_Reminder):
"""Main GUI."""
def __init__(self):
"""Setup the GUI."""
super().__init__()
self.setupUi(self)
self.setup()
def setup(self):
"""Called in '__init__()'. Contains most initializing code."""
self.label.setScaledContents(True)
# Images and sounds that make people relax.
# Showed or played when the user is resting.
self.imgs = [f"Images\\{i}.jpg" for i in range(5)]
self.sounds = ["Sounds\\day.wav", "Sounds\\river.wav"]
self.playSound = True # If true, plays music when window shown.
self.resting = False
self.firstTime = True # Special bool for preventing noise.
self.timer = QTimer()
self.t1, self.t2 = 300, 1500 # Time unit: second.
self.chosenimg = QPixmap(self.imgs[0]) # Image that's used.
self.chosensound = QSound(self.sounds[0]) # Sound that's used.
# Start.
self.countdown2()
self.process()
def process(self):
"""Some things require constant manipulation."""
self.timer.singleShot(50, self.process)
if self.playSound and self.resting:
if self.firstTime:
# User resting, and would like some music.
self.chosensound.play()
# Prevent sound from playing again and again too quickly.
self.firstTime = False
else:
self.chosensound.stop()
self.firstTime = True
def countdown1(self):
"""A timer that counts 'self.t1' * 1sec, which is the time between two rests."""
self.timer.singleShot(self.t1 * 1000, self.countdown2)
# Choose the image and sound to use.
self.chosenimg = QPixmap(random.choice(self.imgs))
self.chosensound = QSound(random.choice(self.sounds))
self.chosensound.setLoops(QSound.Infinite)
# Show image. Sound played in 'self.process'.
self.label.setPixmap(self.chosenimg)
# Fill the whole screen with the main window.
self.showMaximized()
# User is relaxing(resting).
self.resting = True
def countdown2(self):
"""A timer that counts 'self.t2' * 1sec, which is the time of a rest."""
# End sound in 'self.process'.
# User can start using the computer now.
self.timer.singleShot(self.t2 * 1000, self.countdown1)
# Hide the main window.
self.hide()
# User isn't resting now.
self.resting = False
def keyReleaseEvent(self, event):
if event.key() == Qt.Key_Up and self.t2 <= 2850:
self.t2 += 150
elif event.key() == Qt.Key_Down and self.t2 <= 300:
self.t2 -= 150
class Tray(QSystemTrayIcon):
"""This program will show an icon on the system tray."""
def __init__(self):
"""Initialize the program."""
super().__init__() # Setup parent(s).
self.setup()
self.show() # Show the icon.
def setup(self):
# Use the img 'sit.png' for the icon.
self.setIcon(QIcon("sit.png"))
# Setup tool tip(text shown when mouse cursor touches icon).
self.setToolTip("Kitten")
# The sounds played after icon was clicked.
self.sound1 = QSound("sound1.wav")
self.sound2 = QSound("sound2.wav")
# Setup menu.
self.addMenu()
# Connect to main window.
self.window = Reminder()
def addMenu(self):
"""Setup a menu(list of options shown when icon is clicked)."""
self.menu = QMenu() # Create an empty menu.
# Create some actions.
acHideWindow = QAction("Hide Main Window",
self, triggered=self.hideWindow)
acShowWindow = QAction("Show Main Window",
self, triggered=self.showWindow)
acLeave = QAction("Leave", self, triggered=self.leave)
acSound = QAction("Enable Sound", self, triggered=self.enableSound)
acQuiet = QAction("Disable Sound", self, triggered=self.disableSound)
# Add them to the menu 'self.menu'.
self.menu.addActions([acShowWindow ,acHideWindow, acSound, acQuiet, acLeave])
# Show menu.
self.setContextMenu(self.menu)
def hideWindow(self):
self.window.countdown2()
self.sound1.play()
def showWindow(self):
self.window.countdown1()
self.sound2.play()
def leave(self):
self.hide()
self.window.hide()
sys.exit()
def enableSound(self):
self.window.playSound = True
def disableSound(self):
self.window.playSound = False
if __name__ == "__main__":
app = QApplication(sys.argv)
tray = Tray() # Create an instance of 'Tray'.
sys.exit(app.exec_()) # Leave safely.
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn