Python 小车移动动画:后台运行、托盘图标、速度调节
以下是一个符合要求的 Python 程序示例:
import tkinter as tk
import time
from PIL import ImageTk, Image
import win32gui
import win32con
import sys
import os
import ctypes
from tkinter import messagebox
class CarApp:
def __init__(self, root):
self.root = root
self.root.title('Car Moving App')
self.root.protocol('WM_DELETE_WINDOW', self.on_close)
self.root.iconbitmap('car_icon.ico')
self.root.withdraw()
# 创建托盘图标
self.tray_icon = 'car_icon.ico'
self.hicon = ctypes.windll.shell32.Shell_NotifyIconW
self.hicon_id = 0
self.message_map = {
win32con.WM_DESTROY: self.on_destroy,
win32con.WM_COMMAND: self.on_command,
win32con.WM_USER + 20: self.on_tray_notify,
}
self.create_tray_icon()
# 加载小车图片
self.car_image = Image.open('car.png')
self.car_image = self.car_image.resize((100, 100), Image.ANTIALIAS)
self.car_photo = ImageTk.PhotoImage(self.car_image)
# 创建画布并绘制小车
self.canvas = tk.Canvas(self.root, width=800, height=600, bg='white')
self.canvas.pack()
self.car = self.canvas.create_image(0, 0, image=self.car_photo, anchor=tk.NW)
# 创建速度调节按钮
self.speed_var = tk.StringVar()
self.speed_var.set('Speed: 1x')
self.speed_label = tk.Label(self.root, textvariable=self.speed_var)
self.speed_label.pack()
self.speed_btn1 = tk.Button(self.root, text='2x', command=lambda: self.change_speed(2))
self.speed_btn1.pack()
self.speed_btn2 = tk.Button(self.root, text='3x', command=lambda: self.change_speed(3))
self.speed_btn2.pack()
# 设置小车移动参数
self.x = 0
self.y = 0
self.speed = 1
# 启动小车移动
self.move_car()
def move_car(self):
while True:
self.canvas.move(self.car, self.speed, self.speed)
self.canvas.update()
time.sleep(0.05 / self.speed)
self.x += self.speed
self.y += self.speed
if self.x >= 700 or self.y >= 500:
break
def change_speed(self, speed):
self.speed = speed
self.speed_var.set('Speed: {}x'.format(speed))
def on_close(self):
self.root.withdraw()
self.root.iconify()
self.show_tray_icon()
def create_tray_icon(self):
self.hicon_id = self.hicon(
win32gui.GetForegroundWindow(),
win32con.NIM_ADD,
(win32con.NIF_ICON | win32con.NIF_MESSAGE | win32con.NIF_TIP, self.hicon_id, win32con.WM_USER + 20, self.tray_icon, 'Car App')
)
menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu(menu, win32con.MF_STRING, 1, '恢复')
win32gui.AppendMenu(menu, win32con.MF_STRING, 2, '退出')
win32gui.SetMenuDefaultItem(menu, 1)
win32gui.Shell_NotifyIcon(win32con.NIM_ADD, (self.hicon_id, 0, win32gui.NIF_MESSAGE | win32gui.NIF_ICON | win32gui.NIF_TIP, win32con.WM_USER + 20, self.hicon_id, self.tray_icon, 'Car App', menu))
def show_tray_icon(self):
win32gui.Shell_NotifyIcon(win32con.NIM_MODIFY, (self.hicon_id, 0, win32gui.NIF_MESSAGE | win32gui.NIF_ICON | win32gui.NIF_TIP, win32con.WM_USER + 20, self.hicon_id, self.tray_icon, 'Car App'))
def on_destroy(self, hwnd, msg, wparam, lparam):
win32gui.Shell_NotifyIcon(win32con.NIM_DELETE, (self.hicon_id, 0))
def on_command(self, hwnd, msg, wparam, lparam):
id = win32gui.LOWORD(wparam)
if id == 1:
self.root.deiconify()
self.root.lift()
self.root.focus_force()
self.hicon_id = 0
win32gui.Shell_NotifyIcon(win32con.NIM_DELETE, (self.hicon_id, 0))
elif id == 2:
self.root.destroy()
sys.exit()
def on_tray_notify(self, hwnd, msg, wparam, lparam):
if lparam == win32con.WM_LBUTTONDBLCLK:
self.root.deiconify()
self.root.lift()
self.root.focus_force()
self.hicon_id = 0
win32gui.Shell_NotifyIcon(win32con.NIM_DELETE, (self.hicon_id, 0))
elif lparam == win32con.WM_RBUTTONUP:
menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu(menu, win32con.MF_STRING, 1, '恢复')
win32gui.AppendMenu(menu, win32con.MF_STRING, 2, '退出')
pos = win32gui.GetCursorPos()
win32gui.SetMenuDefaultItem(menu, 1)
win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, hwnd, None)
win32gui.PostMessage(hwnd, win32con.WM_NULL, 0, 0)
if __name__ == '__main__':
root = tk.Tk()
app = CarApp(root)
root.mainloop()
在此示例中,我们使用了 Tkinter 库来创建窗口和画布,并使用 PIL 库加载和显示小车图片。我们还使用了 win32gui 和 win32con 库来实现程序的后台运行和托盘图标功能。
请注意,此示例中的图标文件 'car_icon.ico' 和小车图片文件 'car.png' 应该与 Python 脚本文件位于同一目录下。如果图标文件和图片文件的路径不同,请相应地修改图标和图片的路径。
此外,该示例使用了第三方库 PIL 来处理图片,因此需要确保已安装 PIL 库。可以使用以下命令来安装 PIL 库:
pip install pillow
请根据实际需求对程序进行修改和调整,以适应特定的窗口大小、速度范围和图标文件等。
原文地址: http://www.cveoy.top/t/topic/begl 著作权归作者所有。请勿转载和采集!