4

GUI 点击响应函数的参数传递问题

 2 years ago
source link: https://www.v2ex.com/t/849198
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Python

GUI 点击响应函数的参数传递问题

  lon91ong · 11 小时 1 分钟前 · 269 次点击

测试源代码如下:

# -*- coding: utf-8 -*-

from os import path
from time import sleep
from winsystray import SysTrayIcon
from winsystray.win32_adapter import NIIF_USER, NIIF_LARGE_ICON, MFS_DISABLED

app_root = path.dirname(path.realpath(__file__))

def on_right_click(systray):
    build_menu(systray)
    systray._show_menu()

def on_quit(systray):
    global running
    running = False

def on_select(systray):
    '''
    这里怎么知道点击的是哪个菜单呢?

    '''
    
def build_menu(systray):
    global last_main_menu, labs
    
    #print(labs.values())
    main_menu = []
    for k in labs.keys():
        main_menu.append((k, 'pass', MFS_DISABLED))
        for j in labs[k]:
            main_menu.append(('   '+j['Name'], on_select)) # 这里的响应函数怎么传参数呢? 
        main_menu.append((None, '-'))
    main_menu.append((None, '-'))
    main_menu = tuple(main_menu)
    if main_menu != last_main_menu:
        systray.update(menu=main_menu)
        last_main_menu = main_menu

def balloons_info(text, title='通知'):
    my_tray.show_balloon(text, title, NIIF_USER | NIIF_LARGE_ICON)
    
last_main_menu = None
labs={'Sort 1':[{'Name':'Jack', 'Args':'8866'},{'Name':'Bob', 'Args':'9900'}],'Sort 2':[{'Name':'Jams', 'Args':'8686'},{'Name':'John', 'Args':'9090'}]} 
quit_item = '退出', on_quit
icon_pth = path.join(app_root, 'favicon.ico')
my_tray = SysTrayIcon(icon_pth, '测试响应', None, quit_item,
                            left_click=None, right_click=on_right_click)
my_tray.start()

sleep(0.1)
balloons_info('响应测试已经启动。\n\n 右键选择菜单')
running = True
while running:
    sleep(2)

效果如图所示

效果图

###问题是

响应函数如何接收参数呢? 或者 响应函数怎么知道点击的是哪个菜单呢?

引用的库 winsystray参见


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK