赞
踩
效果如下图所示
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 )
桌面代码通过获取动态图每一帧输出实现移动
看完代码不能理解的可以私信~~~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。