从 pysimplegui 的列表框中取消选择项目

Unselect item from listbox from pysimplegui

这个问题很简单。当我从列表框中单击一个项目时,它会打开另一个 window,其中包含信息和按钮。但是,在关闭 Window 并点击搜索按钮(名称中有或没有值)后,它将再次打开 window,因为我认为它仍处于选中状态。以下是我正在使用的简化版本和可运行程序。

更新:使用 python 版本 3.8.2 pysimplegui 版本 4.55.1

import PySimpleGUI as sg
import pandas as pd
import numpy as np

name = ''
info_string = '' #created in create_string() to update text in secondary_gui()
list_index = 0 #created in user() to update user information in add_point()
choices = [] #created in search() to update listbox in main_gui()
index = [] #created in main_gui() to get that users information from dataframe by cross refrencing name
list_info = [] #created in user() to get list of that users information
df = pd.DataFrame(columns=['name', 'points'],
             data=np.array([['James', 2],
                            ['josh', 12],
                            ['charles', 5]
                            ]))

def maingui():
    global name
    global choices
    global index

    layout = [[sg.Text('name', size=(6, 1)), sg.Input(key='-Name-')],
              [sg.Button('Search'), sg.Button('Add user'), sg.Button('Close')],
              [sg.Listbox(choices, size=(51, len(choices)), key='-CName-', enable_events=True, bind_return_key=True)]
              ]

    window = sg.Window('users', layout)

    while True:
        event, values = window.read()

        name = values['-Name-']

        if event == 'Close' or event == sg.WIN_CLOSED:
            break
        if event == 'Search':
            #find match using name
            search()
            #update listbox choices
            window['-CName-'].update(choices)
        #if event == '-CName-' and len(values['-CName-']):
        if values['-CName-']:
            #check information of user clicked from listbox
            index = values['-CName-']
            user()
            #window['-CName-'].enable_click_events = False
            #values['-CName-'] = False
            secondary_gui()
            #sg.popup('selected', values['-CName-'])



    window.close()

def secondary_gui():
    global info_string
    create_string()
    layout = [[sg.Text(info_string, key='-CInfo-')],
              [sg.Button('Add Point')]]

    window = sg.Window('user Information', layout)
    while True:
        event, values = window.read()

        if event == 'Close' or event == sg.WIN_CLOSED:
            break

    window.close()

def search():
    print('search')
    global name
    global choices
    global df

    df1 = df
    # find name in gsheet
    if name:
        df1 = df1.loc[df1['name'].str.contains(name, case=False)]
        print(name)
        print(df1)
    # create list that GUI can read properly
    cdf = df1.values.tolist()
    choices = cdf

def user():
    print('user')
    global index
    global info_string
    global list_index
    global list_info
    global df
    global name

    # index is nested list, get correct values corresponding to list
    name = index[0][0]

    df1 = df
    # create list based on matching name
    df1 = df1.loc[df1['name'].str.contains(name, case=False)]

    # get user index used for when updating user information(add_point())
    list_index = df1.index  #used in function not shown
    # create list of user to update user values(add_point())
    list_info = df1.stack().to_list()
    print(list_info)

def create_string():
    global info_string
    global list_info
    print('creating string')
    print(list_info)

    # create string for usergui window text
    info_string = ' '.join([str(elem) for elem in list_info])
    print(info_string)

maingui()

我试过设置 values['-CName-'] = False,使用 sg.popup 而不是 window。将第 47 行的 enable_click_events 更新为 False。运气不好。

我检查了 github 上的 listbox demo program,没有这个问题。对我来说最突出的区别是

if event == '-LIST-' and len(values['-LIST-']): 而不是 if values['-CName-']: 但当我将 -LIST- 换成 -CName- 时,这对我来说似乎不起作用。

我的另一个想法是以某种方式使用 tkinter 并在此处使用其功能

是编程逻辑造成的,

        if values['-CName-']:

除了事件sg.WIN_CLOSED'Close'之外,无论什么时候事件都会执行事件循环中的这条语句,所以点击搜索后会弹出二级界面

当你第一次点击搜索按钮时,这个if将为false,所以不会弹出window。

如果指定事件'-CName-'弹出副window,可能需要指定为

        elif event == '-CName-' and values['-CName-']:

这表示已单击列表框并选择了某些项目。

如果您需要从列表框中取消选择项目,

window[listbox_key].update(set_to_index=[])