Binding tkiner “events“ 获取/失去焦点,鼠标右键事件

def s1():
    import PySimpleGUI as sg

    sg.theme('Dark Green 2')

    layout = [  [sg.Text('My Window')],
                [sg.Input(key='-IN1-')],
                [sg.Input(key='-IN2-')],
                [sg.Input('失去焦点事件',key='-IN3-')],
                [sg.Input('是否可见', key='-IN4-',visible=False)],
                [sg.Button('可见',key='-Btn4-')],
                [sg.Button('鼠标左键事件'),
                 sg.Button('鼠标中间键事件'),
                 sg.Button('鼠标右键事件'),
                 sg.Button('双击事件'),
                 sg.Button('Unbind'),sg.Button('Exit')],
                  ]

    window = sg.Window('Window Title', layout, finalize=True)


    #点击页面所有的元素,都会触发
    window.bind("<Button-1>", 'Window Click')

    #正常点击按钮“鼠标右键事件”后,绑定event, 然后右键该元素,事件为'Go +RIGHT CLICK+';左键'Go'
    window['鼠标右键事件'].bind("<Button-3>", ' +RIGHT CLICK+')
    window['鼠标左键事件'].bind("<Button-1>", ' +LEFT CLICK+')
    window['鼠标中间键事件'].bind("<Button-2>", ' +MIDDLE CLICK+')
    window['双击事件'].bind("<Double-Button-1>", ' +Double CLICK+')

    #获取到焦点时,event展示'-IN2- +FOCUS+'
    window['-IN2-'].bind("<FocusIn>", ' +FOCUS+')

    #失去焦点时,event展示'-IN3- +FOCUSOut+'
    window['-IN3-'].bind("<FocusOut>", ' +FOCUSOut+')

    #当点击按钮'可见'时,且绑定的input元素可见时,event展示'-IN4- +Visibility+'
    window['-IN4-'].bind("<Visibility>", ' +Visibility+')



    while True:
        event, values = window.read()
        print(event)
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        if event == 'Unbind':
            window['Go'].unbind('<Button-3>')
        if event == '-Btn4-':
            if  window['-IN4-'].visible:
                window['-IN4-'].update(visible=False)
            else:
                window['-IN4-'].update(visible=True)

    window.close()

s1()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值