PySimpleGui LIST 框中的花括号问题和 Python 3
Curly Brackets issue in a PySimpleGui LIST box and Python 3
我希望有人能帮助我解决 Python3 和 PySimpleGui 遇到的这个问题。我的应用程序中有 PySimpleGui 列表,其中填充了来自 sqlite table 的行。我知道 ListBox 接受 python 列表作为参数。它运行良好,除了当 table 列的值在单词之间有 space 时,列表框用大括号括起该值。如果相同的值只是一个单词,则没有括号。我真的不知道我做错了什么。我认为下面的代码解释得更好。感谢您对此的任何意见。
def populate_dir(group) :
# Populate a List with query results
global people_list
global people_list_idx
people_list = []
thisfilter = group
if thisfilter == 'All' :
sql = 'SELECT * FROM Contacts_Main WHERE ?'
thisfilter = '1'
else :
sql = 'SELECT * FROM Contacts_Main WHERE Status = ?'
lcursor = con_.cursor()
lcursor.execute(sql, (thisfilter,))
result = lcursor.fetchall()
# people_list is a pysimplegui List
# Column [0] in eachrec is a integer primary autoincrement ID
# Column [2] is a sqlite Text field which holds a person full name with spaces betwwen names.
for eachrec in result :
people_list.append([eachrec[0], eachrec[2]])
# The list is then populated like this. Note that names with spaces have undesirable curly brackets.
# "312 John"
# "345 {Robert MacDonald}"
# "387 Aline"
# "422 {Paul Redwood}"
在Get rid of brackets when displaying results from sql query in a list box (Tkinter and Python),
Bryan Oakley:
You're inserting a python list into a listbox, but the insert method expects a string. You are responsible for converting the data to a string before inserting the data. If you don't explicitly do the conversion, tkinter will do the conversion for you. That may yield brackets in the data due to the fact that the underlying tcl interpreter will use its own syntax to preserve the list structure of the original data.
也许你可以先将它们转换成字符串。
import PySimpleGUI as sg
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
data = [
[312, "John"],
[345, "Robert MacDonald"],
[387, "Aline"],
[422, "Paul Redwood"],
]
items = list(map(str, data))
layout = [
[sg.Listbox(items, size=(25, 4), enable_events=True, key='LISTBOX')],
]
window = sg.Window('Title', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
print(event, values)
window.close()
我希望有人能帮助我解决 Python3 和 PySimpleGui 遇到的这个问题。我的应用程序中有 PySimpleGui 列表,其中填充了来自 sqlite table 的行。我知道 ListBox 接受 python 列表作为参数。它运行良好,除了当 table 列的值在单词之间有 space 时,列表框用大括号括起该值。如果相同的值只是一个单词,则没有括号。我真的不知道我做错了什么。我认为下面的代码解释得更好。感谢您对此的任何意见。
def populate_dir(group) :
# Populate a List with query results
global people_list
global people_list_idx
people_list = []
thisfilter = group
if thisfilter == 'All' :
sql = 'SELECT * FROM Contacts_Main WHERE ?'
thisfilter = '1'
else :
sql = 'SELECT * FROM Contacts_Main WHERE Status = ?'
lcursor = con_.cursor()
lcursor.execute(sql, (thisfilter,))
result = lcursor.fetchall()
# people_list is a pysimplegui List
# Column [0] in eachrec is a integer primary autoincrement ID
# Column [2] is a sqlite Text field which holds a person full name with spaces betwwen names.
for eachrec in result :
people_list.append([eachrec[0], eachrec[2]])
# The list is then populated like this. Note that names with spaces have undesirable curly brackets.
# "312 John"
# "345 {Robert MacDonald}"
# "387 Aline"
# "422 {Paul Redwood}"
在Get rid of brackets when displaying results from sql query in a list box (Tkinter and Python),
Bryan Oakley:
You're inserting a python list into a listbox, but the insert method expects a string. You are responsible for converting the data to a string before inserting the data. If you don't explicitly do the conversion, tkinter will do the conversion for you. That may yield brackets in the data due to the fact that the underlying tcl interpreter will use its own syntax to preserve the list structure of the original data.
也许你可以先将它们转换成字符串。
import PySimpleGUI as sg
sg.theme("DarkBlue3")
sg.set_options(font=("Courier New", 12))
data = [
[312, "John"],
[345, "Robert MacDonald"],
[387, "Aline"],
[422, "Paul Redwood"],
]
items = list(map(str, data))
layout = [
[sg.Listbox(items, size=(25, 4), enable_events=True, key='LISTBOX')],
]
window = sg.Window('Title', layout, finalize=True)
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
print(event, values)
window.close()