【问题标题】:Unselect item from listbox from pysimplegui从 pysimplegui 的列表框中取消选择项目
【发布时间】:2021-12-29 20:57:43
【问题描述】:

这个问题很直接。当我从列表框中单击一个项目时,它会打开另一个带有信息和按钮的窗口。但是,在关闭该窗口并点击搜索按钮(名称中有或没有值)后,由于我认为仍然被选中,它将再次打开该窗口。下面是我正在使用的缩短版和可运行程序。

更新:使用 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 而不是窗口。在第 47 行将 enable_click_events 更新为 False。没有运气。

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

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

我的另一个想法是以某种方式使用 tkinter 并在这里使用它的功能

【问题讨论】:

  • 辅助窗口不弹出。什么时候应该采取行动?
  • 在单击列表中的一项后确实会弹出辅助窗口,这是应该的。它也正常关闭。但是如果我在第一个窗口中点击搜索按钮,它会再次弹出。
  • 第二次关闭后。点击搜索按钮不会导致第二个窗口再次打开。第一次和第二次关闭的区别是第一次在列表框中选择了一个项目。 (如果项目被选中,它设置为显示深色背景),第二次不是
  • 无论我做什么,我都无法打开第二个窗口。我注意到了几件事。首先,第 28 行(listbox = window['-CName'],键缺少一个破折号 - 我个人不喜欢在键名前后使用这些破折号,我经常忘记一个或另一个。我全盘大写我的键. 很容易将它们可视化。第二,同样在第 28 行,列表框变量没有链接到任何东西。第三,第 64 行中的值 (event, values = window.read()) 也没有链接到任何东西。
  • 这很奇怪,我只是复制粘贴了上面的代码以确保我没有删除我不应该删除的内容。它的运行就像一直以来一样。第 28 行被注释掉。我忘了删除它,我正在尝试其他东西,但它没有带来任何有用的东西。

标签: python python-3.x listbox listboxitem pysimplegui


【解决方案1】:

是程序逻辑造成的,

        if values['-CName-']:

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

当你第一次点击搜索按钮时,这个if为假,所以没有弹出窗口。

如果指定事件'-CName-'弹出二级窗口,可能需要指定为

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

这意味着单击列表框并选择了一些项目。

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

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

【讨论】:

  • 是的,谢谢,非常感谢!这就像一个魅力,现在到其他错误哈哈
  • set_to_index 正是我想要的,只是没想到将其设置为空列表
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-26
  • 1970-01-01
  • 1970-01-01
  • 2021-11-26
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多