프로그램의 지속적인 실행을 위해 시스템 트레이 응용 프로그램이 생성됩니다. 사용자가 응용 프로그램을 닫을 때마다 작업 표시줄에서 실행 중인 상태를 가져옵니다. 시스템 트레이 애플리케이션을 식별하기 위해 해당 애플리케이션에 이미지나 아이콘을 제공할 수 있습니다.
tkinter 응용 프로그램의 시스템 트레이 아이콘을 만들기 위해 Python에서 pystray 모듈을 사용할 수 있습니다. 응용 프로그램의 시스템 트레이 아이콘을 구성하는 데 사용할 수 있는 많은 기능과 방법이 내장되어 있습니다.
컴퓨터에 pystray를 설치하려면 "pip install pystray를 입력하세요. " 쉘 또는 명령 프롬프트의 명령.
시스템 트레이 아이콘을 만들려면 다음 단계를 따르세요.
-
필요한 라이브러리 가져오기 - Pytray , 파이썬 PIL 또는 베개 .
-
hide_window() 함수 정의 창을 철회하고 시스템 트레이에 아이콘을 제공합니다.
-
두 개의 메뉴 항목 "표시 추가 및 정의 " 및 "종료 ".
-
표시 및 종료 기능을 정의하여 메뉴 항목에 명령을 추가하십시오.
예시
# Import the required libraries
from tkinter import *
from pystray import MenuItem as item
import pystray
from PIL import Image, ImageTk
# Create an instance of tkinter frame or window
win=Tk()
win.title("System Tray Application")
# Set the size of the window
win.geometry("700x350")
# Define a function for quit the window
def quit_window(icon, item):
icon.stop()
win.destroy()
# Define a function to show the window again
def show_window(icon, item):
icon.stop()
win.after(0,win.deiconify())
# Hide the window and show on the system taskbar
def hide_window():
win.withdraw()
image=Image.open("favicon.ico")
menu=(item('Quit', quit_window), item('Show', show_window))
icon=pystray.Icon("name", image, "My System Tray Icon", menu)
icon.run()
win.protocol('WM_DELETE_WINDOW', hide_window)
win.mainloop() 출력
위의 코드를 실행하면 응용 프로그램 창이 나타납니다.

창을 닫아도 작업 표시줄에 시스템 트레이 응용 프로그램으로 표시됩니다.
