如何在 PySimpleGUI 中清空列表框
How to empty out Listbox in PySimpleGUI
我是 PySimpleGUI 的新手,但事实证明它真的像宣传的那样简单。几个小时后,我已经有了一个半途而废的应用程序。
我正在使用列表框来显示从磁盘文件读入的几行项目。当我单击 Show Connections 按钮时,它会读取文件并显示我想要的项目。但是如果我再次点击按钮,它会再次读取文件,现在我在盒子里有两个副本。我想在从下一个磁盘文件读取更新之前清空列表框,以便它始终准确显示文件中的内容。我尝试了 Update 和 set_value 但似乎无法正常工作。
layout_showdata = [
[
sg.Text('Time',size=(10,1)),
sg.Text('Destination',size=(14,1)),
sg.Text('Source',size=(14,1))],
[sg.Listbox(size=(38,10),values=[''],key='_display_')]
]
。
.
.
if event == 'Show Connections':
print('Show Connections')
window['panel_show'].update(visible=True)
window['panel_edit'].update(visible=False)
window['panel_entry'].update(visible=False)
window['_display_'].set_value([]) ***#<==This should do it I thought***
with open('connections.txt','r') as cfile:
schedule=csv.reader(cfile,dialect="pipes")
for row in schedule:
items.append(row[0]+':'+row[1]+':'+row[2]+' '+row[3]+' '+row[4])
print(items[itemnum])
itemnum+=1
window.FindElement('_display_').Update(items)
我确信我遗漏了一些简单的东西,如果能得到任何帮助,我将不胜感激。
[编辑] 添加了一些 运行 代码来说明发生了什么。按下按钮时不会清除列表框,它只是再次读取文件并添加到已经存在的内容中:
import PySimpleGUI as sg
import csv
items = []
itemnum = 0
csv.register_dialect('pipes', delimiter='|')
file = [
'01|23|45|12345678|87654321',
'04|35|23|24680864|08642468',
'01|23|45|12345678|87654321',
'04|35|23|24680864|08642468',
'01|23|45|12345678|87654321',
'23|23|23|12341234|43214321'
]
layout_showdata = [
[
sg.Text('Time',size=(10,1)),
sg.Text('Destination',size=(14,1)),
sg.Text('Source',size=(14,1))],
[sg.Listbox(size=(38,10),values=[''],key='_display_')],
[sg.Button('Show Connections')]
]
window = sg.Window('XY Scheduler', layout_showdata)
while True:
event, values = window.Read(timeout=1)
if event in (None, 'Quit'):
break
#Show Existing Connections
if event == 'Show Connections':
print('Show Connections')
window['_display_'].update([])
schedule=csv.reader(file,dialect="pipes")
for row in schedule:
items.append(row[0]+':'+row[1]+':'+row[2]+' '+row[3]+' '+row[4])
print(items[itemnum])
itemnum+=1
window.FindElement('_display_').Update(items)
如果要更改值,则需要更新方法。你会发现它有一个值参数。调用参考文档或文档字符串会对您有很大帮助,但一般使用的经验法则是 update
是更改元素某些内容的方法。
您已经为其他元素调用了它。您只需要为列表框调用它。
window['_display_'].update([])
set_values 调用在文档中有这样的描述:
“设置列表框突出显示的选项”
它设置已经 select 的内容,而不是您必须 select 的选择。
[编辑]
以下是如何删除列表框中所有条目的完整示例。也许我误解了这个问题。
import PySimpleGUI as sg
def main():
layout = [ [sg.Text('My Window')],
[sg.Listbox([1,2,3,4,5], size=(5,3), k='-LB-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Go':
window['-LB-'].update([])
window.close()
if __name__ == '__main__':
main()
我是 PySimpleGUI 的新手,但事实证明它真的像宣传的那样简单。几个小时后,我已经有了一个半途而废的应用程序。
我正在使用列表框来显示从磁盘文件读入的几行项目。当我单击 Show Connections 按钮时,它会读取文件并显示我想要的项目。但是如果我再次点击按钮,它会再次读取文件,现在我在盒子里有两个副本。我想在从下一个磁盘文件读取更新之前清空列表框,以便它始终准确显示文件中的内容。我尝试了 Update 和 set_value 但似乎无法正常工作。
layout_showdata = [
[
sg.Text('Time',size=(10,1)),
sg.Text('Destination',size=(14,1)),
sg.Text('Source',size=(14,1))],
[sg.Listbox(size=(38,10),values=[''],key='_display_')]
]
。 . .
if event == 'Show Connections':
print('Show Connections')
window['panel_show'].update(visible=True)
window['panel_edit'].update(visible=False)
window['panel_entry'].update(visible=False)
window['_display_'].set_value([]) ***#<==This should do it I thought***
with open('connections.txt','r') as cfile:
schedule=csv.reader(cfile,dialect="pipes")
for row in schedule:
items.append(row[0]+':'+row[1]+':'+row[2]+' '+row[3]+' '+row[4])
print(items[itemnum])
itemnum+=1
window.FindElement('_display_').Update(items)
我确信我遗漏了一些简单的东西,如果能得到任何帮助,我将不胜感激。
[编辑] 添加了一些 运行 代码来说明发生了什么。按下按钮时不会清除列表框,它只是再次读取文件并添加到已经存在的内容中:
import PySimpleGUI as sg
import csv
items = []
itemnum = 0
csv.register_dialect('pipes', delimiter='|')
file = [
'01|23|45|12345678|87654321',
'04|35|23|24680864|08642468',
'01|23|45|12345678|87654321',
'04|35|23|24680864|08642468',
'01|23|45|12345678|87654321',
'23|23|23|12341234|43214321'
]
layout_showdata = [
[
sg.Text('Time',size=(10,1)),
sg.Text('Destination',size=(14,1)),
sg.Text('Source',size=(14,1))],
[sg.Listbox(size=(38,10),values=[''],key='_display_')],
[sg.Button('Show Connections')]
]
window = sg.Window('XY Scheduler', layout_showdata)
while True:
event, values = window.Read(timeout=1)
if event in (None, 'Quit'):
break
#Show Existing Connections
if event == 'Show Connections':
print('Show Connections')
window['_display_'].update([])
schedule=csv.reader(file,dialect="pipes")
for row in schedule:
items.append(row[0]+':'+row[1]+':'+row[2]+' '+row[3]+' '+row[4])
print(items[itemnum])
itemnum+=1
window.FindElement('_display_').Update(items)
如果要更改值,则需要更新方法。你会发现它有一个值参数。调用参考文档或文档字符串会对您有很大帮助,但一般使用的经验法则是 update
是更改元素某些内容的方法。
您已经为其他元素调用了它。您只需要为列表框调用它。
window['_display_'].update([])
set_values 调用在文档中有这样的描述: “设置列表框突出显示的选项”
它设置已经 select 的内容,而不是您必须 select 的选择。
[编辑] 以下是如何删除列表框中所有条目的完整示例。也许我误解了这个问题。
import PySimpleGUI as sg
def main():
layout = [ [sg.Text('My Window')],
[sg.Listbox([1,2,3,4,5], size=(5,3), k='-LB-')],
[sg.Button('Go'), sg.Button('Exit')] ]
window = sg.Window('Window Title', layout)
while True: # Event Loop
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Go':
window['-LB-'].update([])
window.close()
if __name__ == '__main__':
main()