请问我的代码为什么不能按预期执行
代码如下:
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox as msgbox
try:
import pystray as stray
from PIL import Image
from pystray import MenuItem, Menu
except:
from pip import main
main(['install', 'pystray'])
main(['install', 'Pillow'])
import pystray as stray
from PIL import Image
from pystray import MenuItem, Menu
del main
import threading
if 'beta' in __version__:
msgbox.showinfo('提示', '当前为测试版本,请勿小心使用!')
if 'FENG GOU' in __version__:
msgbox.showinfo('提示', '当前为疯狗版本,请勿小心使用!')
LOGO_PATH = 'D:\\code\\Python MC 服务器自动卖线\\icon.ico'
IMAGE = Image.open(LOGO_PATH)
TITLE = 'Python MC 服务器自动卖东西'
win = tk.Tk()
SCREENWIDTH = win.winfo_screenwidth()
SCREENHEIGHT = win.winfo_screenheight()
WIDTH = 500
HEIGHT = 500
LEFT = (SCREENWIDTH - WIDTH) / 2
TOP = (SCREENHEIGHT - HEIGHT) / 2
win.geometry(f'{WIDTH}x{HEIGHT}+{int(LEFT)}+{int(TOP)}')
win.resizable(False, False)
win.title(TITLE)
win.iconbitmap(LOGO_PATH)
win.attributes('-topmost', 'false')
TOPMOST = False
TOPMOST_TEXT = '置顶'
def quit_window(icon):
icon.stop()
win.destroy()
def show_window():
win.deiconify()
def hide_window():
win.iconify()
def sicky_window():
global TOPMOST
global TOPMOST_TEXT
if TOPMOST:
win.attributes('-topmost', 'false')
TOPMOST = False
TOPMOST_TEXT = '置顶'
icon.update_menu()
else:
win.attributes('-topmost', 'true')
TOPMOST = True
TOPMOST_TEXT = '取消置顶'
icon.update_menu()
menu = [MenuItem('显示', show_window, default=True), MenuItem('隐藏', hide_window),
Menu.SEPARATOR, MenuItem(TOPMOST_TEXT, sicky_window),
Menu.SEPARATOR, MenuItem('退出', quit_window)]
icon = stray.Icon('icon', IMAGE, TITLE, menu=menu)
threading.Thread(target=icon.run, daemon=True).start()
win.mainloop()
它无法改变托盘的菜单项文本