首页 > 分享 > 用tkinter写一个桌面宠物

用tkinter写一个桌面宠物

目录

桌宠是什么

tkinter是什么

代码

桌宠是什么

桌宠,即桌面宠物,又称电子宠物。属于休闲类小游戏,可存在于任何人的电脑桌面上,饲养它,和它玩耍,看它成长,很有乐趣,无论小孩大人都会喜欢的小型休闲娱乐游戏。

tkinter是什么

Python的标准Tk GUI工具包的接口

Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口.

Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macintosh系统里.Tk8.0的后续版本可以通过ttk实现本地窗口风格,并良好地运行在绝大多数平台中.

此外,tkinter.tix提供了更丰富的窗口组件。

代码

import tkinter

import os

import random

from platform import system

class Pet:

def __init__(self):

self.root = tkinter.Tk()

self.delay = 170

self.pixels_from_right = 200

self.pixels_from_bottom = 200

self.move_speed = 6

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)]

)

self.root.overrideredirect(True)

if system() == 'Windows':

self.root.wm_attributes('-transparent', 'black')

else:

self.root.wm_attributes('-transparent', True)

self.root.config(bg='systemTransparent')

self.root.attributes('-topmost', True)

self.root.bind("<Key>", self.onKeyPress)

self.label = tkinter.Label(self.root, bd=0, bg='black')

if system() != 'Windows':

self.label.config(bg='systemTransparent')

self.label.pack()

screen_width = self.root.winfo_screenwidth()

screen_height = self.root.winfo_screenheight()

self.min_width = 10

self.max_width = screen_width - 110

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):

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

self.root.attributes('-topmost', True)

animation_arr = self.animation[curr_animation]

frame = animation_arr[i]

self.label.configure(image=frame)

if curr_animation in ('walk_left', 'walk_right'):

self.move_window(curr_animation)

i += 1

if i == len(animation_arr):

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') or event.char == "":

self.quit()

if event.char in ("w", "W"):

if self.curr_height >= 0:

self.curr_height -= 1

if event.char in ("s", "S"):

if self.curr_height <= 750:

self.curr_height += 1

if event.char in ("a", "A"):

if self.curr_width >= 0:

self.curr_width -= 1

if event.char in ("d", "D"):

if self.curr_width <= self.max_width:

self.curr_width += 1

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')

self.root.mainloop()

def quit(self):

self.root.destroy()

if __name__ == '__main__':

pet = Pet()

pet.run()

GitHub上一位大佬写的

https://github.com/tommyli3318/desktop-pet/blob/master/main.py

运行效果:

...

相关知识

如何用python写一个桌面宠物
用Python实现自制桌面宠物,变出一个桌面小挂件
用Python写个桌面挂件,手把手带你做只桌面宠物~
用python写个桌面挂件
用Python制作桌面宠物
用c语言写一个桌面宠物
python桌面宠物模块
怎么用Python制作一个可以聊天的皮卡丘版桌面宠物
Python实现桌面挂件,做一只可爱的桌面宠物~
用JavaScript写一个可以聊天的桌面宠物

网址: 用tkinter写一个桌面宠物 https://m.mcbbbk.com/newsview740676.html

所属分类:萌宠日常
上一篇: 基于python开发的DIY宠物
下一篇: 柠檬桌面宠物免费版下载