当前位置:   article > 正文

分享一个基于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.下方是主要的运行模块(下下方看解析)
  1. class Pet :
  2. def __init__ ( self ) :
  3. self.root = tkinter.Tk ( ) # create window
  4. self.delay = 400 # delay in ms
  5. self.pixels_from_right = 200 # change to move the pet's starting position
  6. self.pixels_from_bottom = 200 # change to move the pet's starting position
  7. self.move_speed = 6 # change how fast the pet moves in pixels
  8. # initialize frame arrays
  9. self.animation = dict (
  10. idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle.gif' ) , format = 'gif -index %i' % i )
  11. for i in range ( 5 ) ] ,
  12. idle_to_sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle-to-sleep.gif' ) ,
  13. format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
  14. sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep.gif' ) , format = 'gif -index %i' % i )
  15. for i in range ( 3 ) ] * 3 ,
  16. sleep_to_idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep-to-idle.gif' ) ,
  17. format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
  18. walk_left = [
  19. tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-left.gif' ) , format = 'gif -index %i' % i )
  20. for i in range ( 8 ) ] ,
  21. walk_right = [
  22. tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-right.gif' ) , format = 'gif -index %i' % i )
  23. for i in range ( 8 ) ]
  24. )
  25. def get_file_type ( fpath ) :
  26. """
  27. 获取文件类型
  28. :param fpath: 文件路径
  29. :return: 文件类型
  30. """
  31. if os.path.isdir ( fpath ) :
  32. return 'dir'
  33. ext = os.path.splitext ( fpath ) [ 1 ] [ 1 : ].lower ( )
  34. if ext in [ 'doc' , 'docx' , 'pdf' , 'txt' ] :
  35. return 'document'
  36. elif ext in [ 'png' , 'jpg' , 'jpeg' , 'bmp' , 'gif' ] :
  37. return 'image'
  38. elif ext in [ 'mp3' , 'wav' , 'wma' , 'ogg' , 'flac' ] :
  39. return 'music'
  40. elif ext in [ 'mp4' , 'avi' , 'wmv' , 'mkv' , 'flv' ] :
  41. return 'video'
  42. else :
  43. return 'other'
  44. def sort_files_on_desktop ( ) :
  45. """
  46. 整理桌面上的文件夹和快捷方式
  47. """
  48. # 获取桌面路径
  49. desktop_path = os.path.join ( os.path.expanduser ( "~" ) , "Desktop" )
  50. # 遍历桌面上的文件和文件夹
  51. for file_name in os.listdir ( desktop_path ) :
  52. file_path = os.path.join ( desktop_path , file_name )
  53. file_type = get_file_type ( file_path )
  54. # 根据文件类型创建文件夹或将文件移动到已有的文件夹中
  55. if file_type == 'dir' :
  56. continue
  57. elif file_type == 'document' :
  58. folder_path = os.path.join ( desktop_path , 'documents' )
  59. if not os.path.exists ( folder_path ) :
  60. os.mkdir ( folder_path )
  61. elif file_type == 'image' :
  62. folder_path = os.path.join ( desktop_path , 'images' )
  63. if not os.path.exists ( folder_path ) :
  64. os.mkdir ( folder_path )
  65. elif file_type == 'music' :
  66. folder_path = os.path.join ( desktop_path , 'music' )
  67. if not os.path.exists ( folder_path ) :
  68. os.mkdir ( folder_path )
  69. elif file_type == 'video' :
  70. folder_path = os.path.join ( desktop_path , 'videos' )
  71. if not os.path.exists ( folder_path ) :
  72. os.mkdir ( folder_path )
  73. else :
  74. folder_path = os.path.join ( desktop_path , 'others' )
  75. if not os.path.exists ( folder_path ) :
  76. os.mkdir ( folder_path )
  77. shutil.move ( file_path , os.path.join ( folder_path , file_name ) )
  78. def zhengli ( ) :
  79. """整理桌面上的文件夹和快捷方式"""
  80. sort_files_on_desktop ( )
  81. def RightClicked ( event ) :
  82. self.menu.post ( event.x_root , event.y_root )
  83. def add():
  84. os.system ( "taskmgr" )
  85. def guanji():
  86. os.system('shutdown -s -f -t 10')
  87. def yinyue():
  88. music_player = tkinter.Tk ( )
  89. music_player.title ( "音乐播放器" )
  90. screenWidth = music_player.winfo_screenwidth ( )
  91. screenHeight = music_player.winfo_screenheight ( )
  92. width = 200
  93. height = 200
  94. left = (screenWidth - width) / 2
  95. top = (screenHeight - height) / 2
  96. width = music_player.winfo_screenwidth ( )
  97. height = music_player.winfo_screenheight ( )
  98. music_player.geometry (
  99. "%dx%d+%d+%d" % (int ( width / 2 ) , int ( height / 2 ) , int ( width / 4 ) , int ( height / 4 )) )
  100. directory = askdirectory ( )
  101. os.chdir ( directory )
  102. song_list = os.listdir ( )
  103. play_list = tkinter.Listbox ( music_player , font = "Helvetica 12 bold" , bg = 'SkyBlue1' ,
  104. selectmode = tkinter.SINGLE )
  105. def play ( ) :
  106. pygame.mixer.music.load ( play_list.get ( t.ACTIVE ) )
  107. var.set ( play_list.get ( t.ACTIVE ) )
  108. pygame.mixer.music.play ( )
  109. # 停止
  110. def stop ( ) :
  111. pygame.mixer.music.stop ( )
  112. # 暂停
  113. def pause ( ) :
  114. pygame.mixer.music.pause ( )
  115. # 取消暂停
  116. def unpause ( ) :
  117. pygame.mixer.music.unpause ( )
  118. for item in song_list :
  119. pos = 0
  120. play_list.insert ( pos , item )
  121. pos += 1
  122. pygame.init ( )
  123. pygame.mixer.init ( )
  124. Button1 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "播放" ,
  125. command = play , bg = "SeaGreen1" , fg = "white" )
  126. Button2 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "停止" ,
  127. command = stop , bg = "red" , fg = "white" )
  128. Button3 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" , text = "暂停" ,
  129. command = pause , bg = "Orchid1" , fg = "white" )
  130. Button4 = tkinter.Button ( music_player , width = 5 , height = 3 , font = "Helvetica 12 bold" ,
  131. text = "取消暂停" , command = unpause , bg = "Yellow4" , fg = "white" )
  132. var = tkinter.StringVar ( )
  133. song_title = tkinter.Label ( music_player , font = "Helvetica 12 bold" , textvariable = var )
  134. song_title.pack ( )
  135. Button1.pack ( fill = "x" )
  136. Button2.pack ( fill = "x" )
  137. Button3.pack ( fill = "x" )
  138. Button4.pack ( fill = "x" )
  139. play_list.pack ( fill = "both" , expand = "yes" )
  140. music_player.mainloop ( )
  141. self.root.bind ( "<Button-3>" , RightClicked )
  142. self.menu = Menu ( self.root ,
  143. tearoff = False ,
  144. )
  145. def exit ( ) :
  146. self.root.destroy ( )
  147. self.menu.add_command ( label = "打开任务管理器",command =add)
  148. self.menu.add_command ( label = "音乐播放窗口",command = yinyue )
  149. self.menu.add_command ( label = "关机",command =guanji)
  150. self.menu.add_command ( label = "整理" , command = zhengli)
  151. self.menu.add_command(label = "关闭",command = exit)
  152. # window configuration
  153. self.root.overrideredirect ( True ) # remove UI
  154. if system ( ) == 'Windows' :
  155. self.root.wm_attributes ( '-transparent' , 'black' )
  156. else : # platform is Mac/Linux
  157. # https://stackoverflow.com/questions/19080499/transparent-background-in-a-tkinter-window
  158. self.root.wm_attributes ( '-transparent' , True ) # do this for mac, but the bg stays black
  159. self.root.config ( bg = 'systemTransparent' )
  160. self.root.attributes ( '-topmost' , True ) # put window on top
  161. self.label = tkinter.Label ( self.root , bd = 0 , bg = 'black' ) # borderless window
  162. if system ( ) != 'Windows' :
  163. self.label.config ( bg = 'systemTransparent' )
  164. self.label.pack ( )
  165. screen_width = self.root.winfo_screenwidth ( ) # width of the entire screen
  166. screen_height = self.root.winfo_screenheight ( ) # height of the entire screen
  167. self.min_width = 10 # do not let the pet move beyond this point
  168. self.max_width = screen_width - 110 # do not let the pet move beyond this point
  169. # change starting properties of the window
  170. self.curr_width = screen_width - self.pixels_from_right
  171. self.curr_height = screen_height - self.pixels_from_bottom
  172. self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )
  173. def update ( self , i , curr_animation ) :
  174. # print("Curently: %s" % curr_animation)
  175. self.root.attributes ( '-topmost' , True ) # put window on top
  176. animation_arr = self.animation [ curr_animation ]
  177. frame = animation_arr [ i ]
  178. self.label.configure ( image = frame )
  179. # move the pet if needed
  180. if curr_animation in ('walk_left' , 'walk_right') :
  181. self.move_window ( curr_animation )
  182. i += 1
  183. if i == len ( animation_arr ) :
  184. # reached end of this animation, decide on the next animation
  185. next_animation = self.getNextAnimation ( curr_animation )
  186. self.root.after ( self.delay , self.update , 0 , next_animation )
  187. else :
  188. self.root.after ( self.delay , self.update , i , curr_animation )
  189. def onLeftClick ( self , event ) :
  190. print ( "detected left click" )
  191. def onRightClick ( self , event ) :
  192. self.quit ( )
  193. def onKeyPress ( self , event ) :
  194. if event.char in ('q' , 'Q') :
  195. self.quit ( )
  196. def move_window ( self , curr_animation ) :
  197. if curr_animation == 'walk_left' :
  198. if self.curr_width > self.min_width :
  199. self.curr_width -= self.move_speed
  200. elif curr_animation == 'walk_right' :
  201. if self.curr_width < self.max_width :
  202. self.curr_width += self.move_speed
  203. self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )
  204. def getNextAnimation ( self , curr_animation ) :
  205. if curr_animation == 'idle' :
  206. return random.choice ( [ 'idle' , 'idle_to_sleep' , 'walk_left' , 'walk_right' ] )
  207. elif curr_animation == 'idle_to_sleep' :
  208. return 'sleep'
  209. elif curr_animation == 'sleep' :
  210. return random.choice ( [ 'sleep' , 'sleep_to_idle' ] )
  211. elif curr_animation == 'sleep_to_idle' :
  212. return 'idle'
  213. elif curr_animation == 'walk_left' :
  214. return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
  215. elif curr_animation == 'walk_right' :
  216. return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
  217. def run ( self ) :
  218. self.root.after ( self.delay , self.update , 0 , 'idle' ) # start on idle
  219. self.root.mainloop ( )
  220. if __name__ == '__main__' :
  221. pet = Pet ( )
  222. pet.run ( )

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

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

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

  1. self.animation = dict (
  2. idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle.gif' ) , format = 'gif -index %i' % i )
  3. for i in range ( 5 ) ] ,
  4. idle_to_sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/idle-to-sleep.gif' ) ,
  5. format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
  6. sleep = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep.gif' ) , format = 'gif -index %i' % i )
  7. for i in range ( 3 ) ] * 3 ,
  8. sleep_to_idle = [ tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/sleep-to-idle.gif' ) ,
  9. format = 'gif -index %i' % i ) for i in range ( 8 ) ] ,
  10. walk_left = [
  11. tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-left.gif' ) , format = 'gif -index %i' % i )
  12. for i in range ( 8 ) ] ,
  13. walk_right = [
  14. tkinter.PhotoImage ( file = os.path.abspath ( 'gifs/walk-right.gif' ) , format = 'gif -index %i' % i )
  15. for i in range ( 8 ) ]
  16. )

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

  1. def move_window ( self , curr_animation ) :
  2. if curr_animation == 'walk_left' :
  3. if self.curr_width > self.min_width :
  4. self.curr_width -= self.move_speed
  5. elif curr_animation == 'walk_right' :
  6. if self.curr_width < self.max_width :
  7. self.curr_width += self.move_speed
  8. self.root.geometry ( '%dx%d+%d+%d' % (100 , 100 , self.curr_width , self.curr_height) )

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

  1. def getNextAnimation ( self , curr_animation ) :
  2. if curr_animation == 'idle' :
  3. return random.choice ( [ 'idle' , 'idle_to_sleep' , 'walk_left' , 'walk_right' ] )
  4. elif curr_animation == 'idle_to_sleep' :
  5. return 'sleep'
  6. elif curr_animation == 'sleep' :
  7. return random.choice ( [ 'sleep' , 'sleep_to_idle' ] )
  8. elif curr_animation == 'sleep_to_idle' :
  9. return 'idle'
  10. elif curr_animation == 'walk_left' :
  11. return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
  12. elif curr_animation == 'walk_right' :
  13. return random.choice ( [ 'idle' , 'walk_left' , 'walk_right' ] )
  14. def run ( self ) :
  15. self.root.after ( self.delay , self.update , 0 , 'idle' ) # start on idle
  16. self.root.mainloop ( )

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

run //主窗口循环输出

3.额外功能模块

  1. def yinyue():
  2. music_player = tkinter.Tk ( )
  3. music_player.title ( "音乐播放器" )
  4. screenWidth = music_player.winfo_screenwidth ( )
  5. screenHeight = music_player.winfo_screenheight ( )
  6. width = 200
  7. height = 200
  8. left = (screenWidth - width) / 2
  9. top = (screenHeight - height) / 2
  10. width = music_player.winfo_screenwidth ( )
  11. height = music_player.winfo_screenheight ( )
  12. music_player.geometry (
  13. "%dx%d+%d+%d" % (int ( width / 2 ) , int ( height / 2 ) , int ( width / 4 ) , int ( height / 4 )) )
  14. directory = askdirectory ( )
  15. os.chdir ( directory )
  16. song_list = os.listdir ( )
  17. play_list = tkinter.Listbox ( music_player , font = "Helvetica 12 bold" , bg = 'SkyBlue1' ,
  18. selectmode = tkinter.SINGLE )

音乐播放模块

  1. def RightClicked ( event ) :
  2. self.menu.post ( event.x_root , event.y_root )
  3. def RightClicked ( event ) :
  4. self.menu.post ( event.x_root , event.y_root )
  5. self.menu.add_command ( label = "打开任务管理器",command =add)
  6. self.menu.add_command ( label = "音乐播放窗口",command = yinyue )
  7. self.menu.add_command ( label = "关机",command =guanji)
  8. self.menu.add_command ( label = "整理" , command = zhengli)
  9. self.menu.add_command(label = "关闭",command = exit)

右键菜单模块

  1. def get_file_type ( fpath ) :
  2. """
  3. 获取文件类型
  4. :param fpath: 文件路径
  5. :return: 文件类型
  6. """
  7. if os.path.isdir ( fpath ) :
  8. return 'dir'
  9. ext = os.path.splitext ( fpath ) [ 1 ] [ 1 : ].lower ( )
  10. if ext in [ 'doc' , 'docx' , 'pdf' , 'txt' ] :
  11. return 'document'
  12. elif ext in [ 'png' , 'jpg' , 'jpeg' , 'bmp' , 'gif' ] :
  13. return 'image'
  14. elif ext in [ 'mp3' , 'wav' , 'wma' , 'ogg' , 'flac' ] :
  15. return 'music'
  16. elif ext in [ 'mp4' , 'avi' , 'wmv' , 'mkv' , 'flv' ] :
  17. return 'video'
  18. else :
  19. return 'other'
  20. def sort_files_on_desktop ( ) :
  21. """
  22. 整理桌面上的文件夹和快捷方式
  23. """
  24. # 获取桌面路径
  25. desktop_path = os.path.join ( os.path.expanduser ( "~" ) , "Desktop" )
  26. # 遍历桌面上的文件和文件夹
  27. for file_name in os.listdir ( desktop_path ) :
  28. file_path = os.path.join ( desktop_path , file_name )
  29. file_type = get_file_type ( file_path )
  30. # 根据文件类型创建文件夹或将文件移动到已有的文件夹中
  31. if file_type == 'dir' :
  32. continue
  33. elif file_type == 'document' :
  34. folder_path = os.path.join ( desktop_path , 'documents' )
  35. if not os.path.exists ( folder_path ) :
  36. os.mkdir ( folder_path )
  37. elif file_type == 'image' :
  38. folder_path = os.path.join ( desktop_path , 'images' )
  39. if not os.path.exists ( folder_path ) :
  40. os.mkdir ( folder_path )
  41. elif file_type == 'music' :
  42. folder_path = os.path.join ( desktop_path , 'music' )
  43. if not os.path.exists ( folder_path ) :
  44. os.mkdir ( folder_path )
  45. elif file_type == 'video' :
  46. folder_path = os.path.join ( desktop_path , 'videos' )
  47. if not os.path.exists ( folder_path ) :
  48. os.mkdir ( folder_path )
  49. else :
  50. folder_path = os.path.join ( desktop_path , 'others' )
  51. if not os.path.exists ( folder_path ) :
  52. os.mkdir ( folder_path )
  53. shutil.move ( file_path , os.path.join ( folder_path , file_name ) )

桌面整理代码

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

关闭窗口

  1. def update ( self , i , curr_animation ) :
  2. # print("Curently: %s" % curr_animation)
  3. self.root.attributes ( '-topmost' , True ) # put window on top
  4. animation_arr = self.animation [ curr_animation ]
  5. frame = animation_arr [ i ]
  6. self.label.configure ( image = frame )
  7. # move the pet if needed
  8. if curr_animation in ('walk_left' , 'walk_right') :
  9. self.move_window ( curr_animation )
  10. i += 1
  11. if i == len ( animation_arr ) :
  12. # reached end of this animation, decide on the next animation
  13. next_animation = self.getNextAnimation ( curr_animation )
  14. self.root.after ( self.delay , self.update , 0 , next_animation )
  15. else :
  16. self.root.after ( self.delay , self.update , i , curr_animation )

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/657536
推荐阅读
相关标签
  

闽ICP备14008679号