分享一个基于gihub大佬制作的桌面宠物软件源码

效果如下图所示

1.项目依赖库,需要提前安装好

import tkinter    //窗口模块
import random    
from platform import system
import os  //系统命令
import tkinter as t
import pygame
from tkinter.filedialog import askdirectory
from tkinter import Menu //菜单
import shutil

2.下方是主要的运行模块(下下方看解析)
class Pet :
    def __init__ ( self ) :
        self.root = tkinter.Tk ( )  # create window
        self.delay = 400  # delay in ms
        self.pixels_from_right = 200  # change to move the pet's starting position
        self.pixels_from_bottom = 200  # change to move the pet's starting position
        self.move_speed = 6  # change how fast the pet moves in pixels

        # initialize frame arrays
        self.animation = dict (
            idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle.gif' ) , format = 'gif -index %i' % i )
                     for i in range ( 5 ) ] ,
            idle_to_sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle-to-sleep.gif' ) ,
                                                   format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
            sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep.gif' ) , format = 'gif -index %i' % i )
                      for i in range ( 3 ) ] * 3 ,
            sleep_to_idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep-to-idle.gif' ) ,
                                                   format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
            walk_left = [
                tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-left.gif' ) , format = 'gif -index %i' % i )
                for i in range ( 8 ) ] ,
            walk_right = [
                tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-right.gif' ) , format = 'gif -index %i' % i )
                for i in range ( 8 ) ]
        )

        def get_file_type ( fpath ) :
            """
            获取文件类型
            :param fpath: 文件路径
            :return: 文件类型
            """
            if os.path.isdir ( fpath ) :
                return 'dir'
            ext = os.path.splitext ( fpath ) [ 1 ] [ 1 : ].lower ( )
            if ext in [ 'doc' , 'docx' , 'pdf' , 'txt' ] :
                return 'document'
            elif ext in [ 'png' , 'jpg' , 'jpeg' , 'bmp' , 'gif' ] :
                return 'image'
            elif ext in [ 'mp3' , 'wav' , 'wma' , 'ogg' , 'flac' ] :
                return 'music'
            elif ext in [ 'mp4' , 'avi' , 'wmv' , 'mkv' , 'flv' ] :
                return 'video'
            else :
                return 'other'

        def sort_files_on_desktop ( ) :
            """
            整理桌面上的文件夹和快捷方式
            """
            # 获取桌面路径
            desktop_path = os.path.join ( os.path.expanduser ( "~" ) , "Desktop" )
            # 遍历桌面上的文件和文件夹
            for file_name in os.listdir ( desktop_path ) :
                file_path = os.path.join ( desktop_path , file_name )
                file_type = get_file_type ( file_path )
                # 根据文件类型创建文件夹或将文件移动到已有的文件夹中
                if file_type == 'dir' :
                    continue
                elif file_type == 'document' :
                    folder_path = os.path.join ( desktop_path , 'documents' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'image' :
                    folder_path = os.path.join ( desktop_path , 'images' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'music' :
                    folder_path = os.path.join ( desktop_path , 'music' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'video' :
                    folder_path = os.path.join ( desktop_path , 'videos' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                else :
                    folder_path = os.path.join ( desktop_path , 'others' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                shutil.move ( file_path , os.path.join ( folder_path , file_name ) )


        def zhengli ( ) :
            """整理桌面上的文件夹和快捷方式"""
            sort_files_on_desktop ( )



        def RightClicked ( event ) :
            self.menu.post ( event.x_root , event.y_root )
        def add():
            os.system ( "taskmgr" )

        def guanji():
            os.system('shutdown -s -f -t 10')



        def yinyue():

            music_player = tkinter.Tk ( )
            music_player.title ( "音乐播放器" )
            screenWidth = music_player.winfo_screenwidth ( )
            screenHeight = music_player.winfo_screenheight ( )
            width = 200
            height = 200
            left = (screenWidth - width) / 2
            top = (screenHeight - height) / 2
            width = music_player.winfo_screenwidth ( )
            height = music_player.winfo_screenheight ( )
            music_player.geometry (
                "%dx%d+%d+%d" % (int ( width / 2 ) , int ( height / 2 ) , int ( width / 4 ) , int ( height / 4 )) )
            directory = askdirectory ( )
            os.chdir ( directory )
            song_list = os.listdir ( )
            play_list = tkinter.Listbox ( music_player , font = "Helvetica 12 bold" , bg = 'SkyBlue1' ,
                                    selectmode = tkinter.SINGLE )

            def play ( ) :
                pygame.mixer.music.load ( play_list.get ( t.ACTIVE ) )
                var.set ( play_list.get ( t.ACTIVE ) )
                pygame.mixer.music.play ( )

                # 停止

            def stop ( ) :
                pygame.mixer.music.stop ( )

                # 暂停

            def pause ( ) :
                pygame.mixer.music.pause ( )

                # 取消暂停

            def unpause ( ) :
                pygame.mixer.music.unpause ( )
            for item in song_list :
                pos = 0
                play_list.insert ( pos , item )
                pos += 1
            pygame.init ( )
            pygame.mixer.init ( )
            Button1 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "播放" ,
                                 command = play , bg = "SeaGreen1" , fg = "white" )
            Button2 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "停止" ,
                                 command = stop , bg = "red" , fg = "white" )
            Button3 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "暂停" ,
                                 command = pause , bg = "Orchid1" , fg = "white" )
            Button4 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" ,
                                 text = "取消暂停" , command = unpause , bg = "Yellow4" , fg = "white" )
            var = tkinter.StringVar ( )
            song_title = tkinter.Label ( music_player , font = "Helvetica 12 bold" , textvariable = var )
            song_title.pack ( )
            Button1.pack ( fill = "x" )
            Button2.pack ( fill = "x" )
            Button3.pack ( fill = "x" )
            Button4.pack ( fill = "x" )
            play_list.pack ( fill = "both" , expand = "yes" )
            music_player.mainloop ( )

        self.root.bind ( "<Button-3>" , RightClicked )
        self.menu = Menu ( self.root ,
                           tearoff = False ,
                           )

        def exit ( ) :
            self.root.destroy ( )

        self.menu.add_command ( label = "打开任务管理器",command =add)
        self.menu.add_command ( label = "音乐播放窗口",command = yinyue )
        self.menu.add_command ( label = "关机",command =guanji)
        self.menu.add_command ( label = "整理" , command = zhengli)
        self.menu.add_command(label = "关闭",command = exit)

        # window configuration
        self.root.overrideredirect ( True )  # remove UI
        if system ( ) == 'Windows' :
            self.root.wm_attributes ( '-transparent' , 'black' )
        else :  # platform is Mac/Linux
            # https://stackoverflow.com/questions/19080499/transparent-background-in-a-tkinter-window
            self.root.wm_attributes ( '-transparent' , True )  # do this for mac, but the bg stays black
            self.root.config ( bg = 'systemTransparent' )

        self.root.attributes ( '-topmost' , True )  # put window on top
        self.label = tkinter.Label ( self.root , bd = 0 , bg = 'black' )  # borderless window
        if system ( ) != 'Windows' :
            self.label.config ( bg = 'systemTransparent' )
        self.label.pack ( )

        screen_width = self.root.winfo_screenwidth ( )  # width of the entire screen
        screen_height = self.root.winfo_screenheight ( )  # height of the entire screen
        self.min_width = 10  # do not let the pet move beyond this point
        self.max_width = screen_width - 110  # do not let the pet move beyond this point

        # change starting properties of the window
        self.curr_width = screen_width - self.pixels_from_right
        self.curr_height = screen_height - self.pixels_from_bottom
        self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )


    def update ( self , i , curr_animation ) :
        # print("Curently: %s" % curr_animation)
        self.root.attributes ( '-topmost' , True )  # put window on top
        animation_arr = self.animation [ curr_animation ]
        frame = animation_arr [ i ]
        self.label.configure ( image = frame )

        # move the pet if needed
        if curr_animation in ('walk_left' , 'walk_right') :
            self.move_window ( curr_animation )

        i += 1
        if i == len ( animation_arr ) :
            # reached end of this animation, decide on the next animation
            next_animation = self.getNextAnimation ( curr_animation )
            self.root.after ( self.delay , self.update , 0 , next_animation )
        else :
            self.root.after ( self.delay , self.update , i , curr_animation )


    def onLeftClick ( self , event ) :
        print ( "detected left click" )

    def onRightClick ( self , event ) :
        self.quit ( )

    def onKeyPress ( self , event ) :
        if event.char in ('q' , 'Q') :
            self.quit ( )

    def move_window ( self , curr_animation ) :
        if curr_animation == 'walk_left' :
            if self.curr_width > self.min_width :
                self.curr_width -= self.move_speed

        elif curr_animation == 'walk_right' :
            if self.curr_width < self.max_width :
                self.curr_width += self.move_speed

        self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )

    def getNextAnimation ( self , curr_animation ) :
        if curr_animation == 'idle' :
            return random.choice ( [ 'idle' , 'idle_to_sleep' , 'walk_left' , 'walk_right' ] )
        elif curr_animation == 'idle_to_sleep' :
            return 'sleep'
        elif curr_animation == 'sleep' :
            return random.choice ( [ 'sleep' , 'sleep_to_idle' ] )
        elif curr_animation == 'sleep_to_idle' :
            return 'idle'
        elif curr_animation == 'walk_left' :
            return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
        elif curr_animation == 'walk_right' :
            return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )

    def run ( self ) :
        self.root.after ( self.delay , self.update , 0 , 'idle' )  # start on idle
        self.root.mainloop ( )



if __name__ == '__main__' :
    pet = Pet ( )
    pet.run ( )

3.对代码的解析(多理解)

self.root = tkinter.Tk ( )  # create window
        self.delay = 400  # delay in ms
        self.pixels_from_right = 200  # change to move the pet's starting position
        self.pixels_from_bottom = 200  # change to move the pet's starting position
        self.move_speed = 6  # change how fast the pet moves in pixels

1.一个宠物桌面移动需要 延时器,速度,左右移动的限制速度

self.animation = dict (
            idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle.gif' ) , format = 'gif -index %i' % i )
                     for i in range ( 5 ) ] ,
            idle_to_sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle-to-sleep.gif' ) ,
                                                   format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
            sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep.gif' ) , format = 'gif -index %i' % i )
                      for i in range ( 3 ) ] * 3 ,
            sleep_to_idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep-to-idle.gif' ) ,
                                                   format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
            walk_left = [
                tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-left.gif' ) , format = 'gif -index %i' % i )
                for i in range ( 8 ) ] ,
            walk_right = [
                tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-right.gif' ) , format = 'gif -index %i' % i )
                for i in range ( 8 ) ]
        )

 动物移动需要的动作,file 后方括号的相对本文件的相对路径(动作gif)

  def move_window ( self , curr_animation ) :
        if curr_animation == 'walk_left' :
            if self.curr_width > self.min_width :
                self.curr_width -= self.move_speed

        elif curr_animation == 'walk_right' :
            if self.curr_width < self.max_width :
                self.curr_width += self.move_speed

        self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )

桌面宠物的移动板块,同时限制了桌面宠物的移动限制,使其不会移出屏幕

 def getNextAnimation ( self , curr_animation ) :
        if curr_animation == 'idle' :
            return random.choice ( [ 'idle' , 'idle_to_sleep' , 'walk_left' , 'walk_right' ] )
        elif curr_animation == 'idle_to_sleep' :
            return 'sleep'
        elif curr_animation == 'sleep' :
            return random.choice ( [ 'sleep' , 'sleep_to_idle' ] )
        elif curr_animation == 'sleep_to_idle' :
            return 'idle'
        elif curr_animation == 'walk_left' :
            return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
        elif curr_animation == 'walk_right' :
            return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )

    def run ( self ) :
        self.root.after ( self.delay , self.update , 0 , 'idle' )  # start on idle
        self.root.mainloop ( )

getnext // 实现桌面宠物移动的连续化 

run //主窗口循环输出

3.额外功能模块

 def yinyue():

            music_player = tkinter.Tk ( )
            music_player.title ( "音乐播放器" )
            screenWidth = music_player.winfo_screenwidth ( )
            screenHeight = music_player.winfo_screenheight ( )
            width = 200
            height = 200
            left = (screenWidth - width) / 2
            top = (screenHeight - height) / 2
            width = music_player.winfo_screenwidth ( )
            height = music_player.winfo_screenheight ( )
            music_player.geometry (
                "%dx%d+%d+%d" % (int ( width / 2 ) , int ( height / 2 ) , int ( width / 4 ) , int ( height / 4 )) )
            directory = askdirectory ( )
            os.chdir ( directory )
            song_list = os.listdir ( )
            play_list = tkinter.Listbox ( music_player , font = "Helvetica 12 bold" , bg = 'SkyBlue1' ,
                                    selectmode = tkinter.SINGLE )

音乐播放模块

 def RightClicked ( event ) :
            self.menu.post ( event.x_root , event.y_root )

def RightClicked ( event ) :
           self.menu.post ( event.x_root , event.y_root )


self.menu.add_command ( label = "打开任务管理器",command =add)
        self.menu.add_command ( label = "音乐播放窗口",command = yinyue )
        self.menu.add_command ( label = "关机",command =guanji)
        self.menu.add_command ( label = "整理" , command = zhengli)
        self.menu.add_command(label = "关闭",command = exit)

右键菜单模块

 def get_file_type ( fpath ) :
            """
            获取文件类型
            :param fpath: 文件路径
            :return: 文件类型
            """
            if os.path.isdir ( fpath ) :
                return 'dir'
            ext = os.path.splitext ( fpath ) [ 1 ] [ 1 : ].lower ( )
            if ext in [ 'doc' , 'docx' , 'pdf' , 'txt' ] :
                return 'document'
            elif ext in [ 'png' , 'jpg' , 'jpeg' , 'bmp' , 'gif' ] :
                return 'image'
            elif ext in [ 'mp3' , 'wav' , 'wma' , 'ogg' , 'flac' ] :
                return 'music'
            elif ext in [ 'mp4' , 'avi' , 'wmv' , 'mkv' , 'flv' ] :
                return 'video'
            else :
                return 'other'

        def sort_files_on_desktop ( ) :
            """
            整理桌面上的文件夹和快捷方式
            """
            # 获取桌面路径
            desktop_path = os.path.join ( os.path.expanduser ( "~" ) , "Desktop" )
            # 遍历桌面上的文件和文件夹
            for file_name in os.listdir ( desktop_path ) :
                file_path = os.path.join ( desktop_path , file_name )
                file_type = get_file_type ( file_path )
                # 根据文件类型创建文件夹或将文件移动到已有的文件夹中
                if file_type == 'dir' :
                    continue
                elif file_type == 'document' :
                    folder_path = os.path.join ( desktop_path , 'documents' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'image' :
                    folder_path = os.path.join ( desktop_path , 'images' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'music' :
                    folder_path = os.path.join ( desktop_path , 'music' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                elif file_type == 'video' :
                    folder_path = os.path.join ( desktop_path , 'videos' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                else :
                    folder_path = os.path.join ( desktop_path , 'others' )
                    if not os.path.exists ( folder_path ) :
                        os.mkdir ( folder_path )
                shutil.move ( file_path , os.path.join ( folder_path , file_name ) )

桌面整理代码

        def exit ( ) :
            self.root.destroy ( )

关闭窗口

    def update ( self , i , curr_animation ) :
        # print("Curently: %s" % curr_animation)
        self.root.attributes ( '-topmost' , True )  # put window on top
        animation_arr = self.animation [ curr_animation ]
        frame = animation_arr [ i ]
        self.label.configure ( image = frame )

        # move the pet if needed
        if curr_animation in ('walk_left' , 'walk_right') :
            self.move_window ( curr_animation )

        i += 1
        if i == len ( animation_arr ) :
            # reached end of this animation, decide on the next animation
            next_animation = self.getNextAnimation ( curr_animation )
            self.root.after ( self.delay , self.update , 0 , next_animation )
        else :
            self.root.after ( self.delay , self.update , i , curr_animation )

桌面代码通过获取动态图每一帧输出实现移动

看完代码不能理解的可以私信~~~

  • 8
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 目前,在GitHub上可以找到许多基于图像识别的Qt系统的源码。这些源码主要分为两大类:一类是基于已有的图像识别算法进行二次开发的系统,另一类是完全自主研发的系统。 对于第一类,很多开发者通过使用Qt框架封装和优化开源图像识别算法,实现了一系列图像识别应用。这些源码提供了诸如人脸识别、物体检测、手势识别等功能的实现。这些系统独立于底层图像识别算法库,通过Qt的强大功能和良好的跨平台性能,为系统提供了友好的用户界面和良好的交互体验。 对于第二类,一些开发者基于Qt自主研发了一整套图像识别系统。这些系统往往包含了图像采集、特征提取、模式匹配等多个流程,并采用了很多成熟的图像算法和深度学习模型。这些源码通常是商业级别的系统,可以应用于各种场景,如智能安防、自动驾驶、智能机器人等。 无论是哪一类的源码,都具有一定的参考和学习价值。通过研究这些源码,可以了解到图像识别技术的应用和实现方式,同时也可以借鉴其中的优秀设计和实现思路。对于想要进行图像识别系统开发的开发者来说,GitHub上的这些源码是宝贵的资源。 总结来说,Qt基于图像识别系统的源码在GitHub上有很多可供参考的项目。开发者可以通过研究这些源码,了解图像识别技术的应用和实现方式,同时也可以借鉴其中的优秀设计和实现思路。 ### 回答2: 在GitHub上可以找到许多基于图像识别系统的Qt源代码项目。Qt是一个功能强大且跨平台的应用程序开发框架,它提供了丰富的图形和用户界面库,非常适合用于开发图像识别相关的应用程序。 这些基于图像识别系统的Qt源码项目通常包含了实现基本图像处理和计算机视觉算法的代码,以及与用户界面交互的界面设计。例如,有些项目使用Qt的图像处理库实现了图像预处理操作,比如灰度化、二值化、滤波等;同时,它们还包括了计算机视觉算法,比如特征提取、目标检测、图像匹配等。 这些项目还常常结合了Qt的图形界面模块,提供了友好的用户界面。通过这些界面,用户可以输入图像,设置识别参数,预览和保存处理后的图像,以及查看和分析识别结果。同时,一些项目还提供了图像标注和训练模块,以便用户可以通过标注和训练自定义模型,提高识别系统的性能。 总的来说,GitHub上的这些基于图像识别系统的Qt源码项目,提供了一种方便快捷的方式,让开发者能够基于这些项目进行二次开发,快速搭建自己的图像识别应用程序。通过使用Qt这个强大的应用程序开发框架,我们可以轻松实现图像处理和计算机视觉算法,并提供友好的用户界面,让用户能够方便地使用和操作识别系统,达到更好的用户体验和效果。 ### 回答3: 有关基于图像识别系统的Qt源码,可以在GitHub上找到很多开源项目。 其中一些受欢迎的图像识别库包括OpenCV、TensorFlow和Caffe等。这些库提供了用于图像处理和机器学习的丰富函数和算法,可以帮助开发者实现图像识别系统。 在GitHub上,你可以搜索这些图像识别库的关键词,然后根据自己的需求选择适合的项目。选择一个活跃的项目可以确保源码的稳定性和更新性。一般来说,活跃的项目会有较多的贡献者和更新日志。 一旦找到符合需求的项目,你就可以从GitHub上克隆源码到本地进行使用和修改。Qt作为一个跨平台的开发框架,可以与这些图像识别库结合使用,为图像识别系统提供用户界面和图像展示功能。 在使用GitHub上的源码时,注意遵守开源协议,遵循项目的授权要求。如果你对项目有改进或者增加功能的想法,也可以向原作者提交贡献,使得项目不断进步发展。 总而言之,通过在GitHub上搜索图像识别相关的开源项目,可以找到满足需求的Qt源码,并进行二次开发或者直接使用,提高开发效率和图像识别系统的质量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小羊写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值