Python實現桌面寵物

2021-10-05 18:12:22 字數 4238 閱讀 6375

基於pyqt5

import os

import cfg

import sys

import random

from pyqt5.qtgui import *

from pyqt5.qtcore import *

from pyqt5.qtwidgets import *

from pyqt5 import qtwidgets, qtgui

'''桌面寵物'''

class desktoppet(qwidget):

def __init__(self, parent=none, **kwargs):

super(desktoppet, self).__init__(parent)

# 初始化

self.setwindowflags(qt.framelesswindowhint|qt.windowstaysontophint|qt.subwindow)

self.setautofillbackground(false)

self.setattribute(qt.wa_translucentbackground, true)

self.repaint()

# 隨機匯入乙個寵物

self.pet_images, iconpath = self.randomloadpetimages()

# 設定退出選項

quit_action = qaction('退出', self, triggered=self.quit)

quit_action.seticon(qicon(iconpath))

self.tray_icon_menu = qmenu(self)

self.tray_icon_menu.addaction(quit_action)

self.tray_icon = qsystemtrayicon(self)

self.tray_icon.seticon(qicon(iconpath))

self.tray_icon.setcontextmenu(self.tray_icon_menu)

self.tray_icon.show()

# 當前顯示的

self.image = qlabel(self)

self.setimage(self.pet_images[0][0])

# 是否跟隨滑鼠

self.is_follow_mouse = false

# 寵物拖拽時避免滑鼠直接跳到左上角

self.mouse_drag_pos = self.pos()

# 顯示

self.resize(128, 128)

self.randomposition()

self.show()

# 寵物動畫動作執行所需的一些變數

self.is_running_action = false

self.action_images =

self.action_pointer = 0

self.action_max_len = 0

# 每隔一段時間做個動作

self.timer = qtimer()

self.timer.timeout.connect(self.randomact)

self.timer.start(500)

'''隨機做乙個動作'''

def randomact(self):

if not self.is_running_action:

self.is_running_action = true

self.action_images = random.choice(self.pet_images)

self.action_max_len = len(self.action_images)

self.action_pointer = 0

self.runframe()

'''完成動作的每一幀'''

def runframe(self):

if self.action_pointer == self.action_max_len:

self.is_running_action = false

self.action_pointer = 0

self.action_max_len = 0

self.setimage(self.action_images[self.action_pointer])

self.action_pointer += 1

'''設定當前顯示的'''

def setimage(self, image):

self.image.setpixmap(qpixmap.fromimage(image))

'''隨機匯入乙個桌面寵物的所有'''

def randomloadpetimages(self):

pet_name = random.choice(list(cfg.pet_actions_map.keys()))

actions = cfg.pet_actions_map[pet_name]

pet_images =

for action in actions:

iconpath = os.path.join(cfg.root_dir, pet_name, 'shime1.png')

return pet_images, iconpath

'''滑鼠左鍵按下時, 寵物將和滑鼠位置繫結'''

def mousepressevent(self, event):

if event.button() == qt.leftbutton:

self.is_follow_mouse = true

self.mouse_drag_pos = event.globalpos() - self.pos()

event.accept()

self.setcursor(qcursor(qt.openhandcursor))

'''滑鼠移動, 則寵物也移動'''

def mousemoveevent(self, event):

if qt.leftbutton and self.is_follow_mouse:

self.move(event.globalpos() - self.mouse_drag_pos)

event.accept()

'''滑鼠釋放時, 取消繫結'''

def mousereleaseevent(self, event):

self.is_follow_mouse = false

self.setcursor(qcursor(qt.arrowcursor))

'''匯入影象'''

def loadimage(self, imagepath):

image = qimage()

image.load(imagepath)

return image

'''隨機到乙個螢幕上的某個位置'''

def randomposition(self):

screen_geo = qdesktopwidget().screengeometry()

pet_geo = self.geometry()

width = (screen_geo.width() - pet_geo.width()) * random.random()

height = (screen_geo.height() - pet_geo.height()) * random.random()

self.move(width, height)

'''退出程式'''

程式設定的是隨機選擇顯示的寵物

做右下角工作列的托盤圖示可以設定寵物退出,或者直接結束python程式

參考

真是大佬,給我開啟新世界的大門

用python實現寵物管理系統

pets 全域性變數 def add pet id input 請輸入寵物編號 name input 請輸入寵物名稱 category input 請輸入寵物種類 price input 請輸入寵物 pet print 恭喜寵物新增成功!def search pet name input 請輸入寵物...

Python之示例買寵物

學習了一點簡單語法,感覺可以做個東西啦 寫了乙個簡單的 買寵物!活學活用,自己學的語法知識結合了一點幽默的氣質。author zachary li 購物系統 while true maxslary input 請輸入您準備消費的最大資產值 n if maxslary.isdigit true if ...

手機桌面寵物 星秀伴侶1 0手機版

眼下最流行的見面問候,好像就是問偷菜了嗎?qq農場剛玩過,好不容易公升級成會員,才有機會玩qq牧場,qq牧場的動畫效果 明顯要 好於開心農場。最近發現有個軟體,把qq牧場的動畫效果,搬到了手機桌面,安裝這個軟體以後,原本靜態的手機桌面變成了動態效果,而且 還有房 屋,胡蘿蔔等背景,跳動的小兔子,頭頂...