无法使用Python在PySimpleGui表中显示MongoDB数据库中的数据
我试图在使用PySimpleGui库创建的windows窗体表中显示MongoDB数据库中的数据。以下代码改编自pySimpleGUI演示程序demo_test_element.py 传递给成功在表中显示数据的代码的数据如下所示:无法使用Python在PySimpleGui表中显示MongoDB数据库中的数据,python,mongodb,pysimplegui,Python,Mongodb,Pysimplegui,我试图在使用PySimpleGui库创建的windows窗体表中显示MongoDB数据库中的数据。以下代码改编自pySimpleGUI演示程序demo_test_element.py 传递给成功在表中显示数据的代码的数据如下所示: [[“狗”、“包”]、[“牛”、“牛群”]] 在第二个示例中,数据被检索到一个游标中,然后放入一个列表中。在显示表格时,我得到一个类型错误:只能将列表(而不是“dict”)连接到列表。当我打印列表类型时,它被描述为,而不是字典。列表中的数据格式与示例程序中的数据格式不
[[“狗”、“包”]、[“牛”、“牛群”]] 在第二个示例中,数据被检索到一个游标中,然后放入一个列表中。在显示表格时,我得到一个类型错误:只能将列表(而不是“dict”)连接到列表。当我打印列表类型时,它被描述为,而不是字典。列表中的数据格式与示例程序中的数据格式不同。看起来是这样的:
[{u-id':ObjectId('5F51B8A5D174EA26FBE2C64'),'name':'Dog','collective':'Pack'},{u-id':ObjectId('5F51B8A5D174EA26FBE2C65'),'name':'牛','collective':'Herd'}] 所以,我想我会尝试改变列表的格式,以与成功的版本相同的方式。我能做到:
[“[‘狗’、‘包’]、“[‘牛’、‘畜群’]” 如果没有双引号,那么它将遵循成功的模式。尝试在表中显示数据时,错误为TypeError:只能将list(而不是“str”)连接到list 我是Python等的新手,我可能走错了方向。我更喜欢pySimpleGUI而不是Tkinter,因为我发现它更简单,但我很乐意接受使用不同GUI库的建议。我习惯于使用RDBMS(主要是Oracle),我选择MongoDB作为一个挑战,同样,我并不完全喜欢它 对于这个问题,我试图使代码尽可能简洁
from pymongo import MongoClient
import PySimpleGUI as sg
# Display the data in a table
def windowStuff(data):
headings = ["Animal","Collective"]
layout = [[sg.Table(values=data[0:][:], headings=headings, max_col_width=25, display_row_numbers=True, num_rows=4, alternating_row_color='red',
key='-TABLE-', row_height=35)]]
window = sg.Window('The Animals', layout,)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
window.close()
sg.theme('Black')
data = [["Dog", "Pack"],["Cattle","Herd"]]
try:
windowStuff(data)
# This works
except Exception:
pass
# Insert and return data into Mongo Database
client = MongoClient('mongodb://localhost:27017/')
db = client.testdb
db.colCreatures.drop()
docCreatures = [ {'name': 'Dog', 'collective': 'Pack'}, {'name': 'Cattle', 'collective': 'Herd'}]
db.colCreatures.insert_many(docCreatures)
curCreatures = db.colCreatures.find()
#Call the function to display the table with data, using a LIST
listCreatures = list(curCreatures)
try:
windowStuff(listCreatures)
except Exception:
# Error is File "..... \PySimpleGUI.py", line 11646, in PackFormIntoFrame
# value = [i + element.StartingRowNumber] + value
# TypeError: can only concatenate list (not "dict") to list
pass
#Call the function to display the table after the list has been manipulated
data =[]
for creature in listCreatures:
strCreature = ('[{0} {1}'.format(creature['name'] +',', creature['collective'] + ']'))
strCreature = strCreature.replace('[', '[' + "'")
strCreature = strCreature.replace(']' , "'" + ']')
strCreature = strCreature.replace(', ', "'" + ',' + "'")
data.append(strCreature)
try:
windowStuff(data)
except Exception:
# Error is File "....\PySimpleGUI.py", line 11646, in PackFormIntoFrame
# value = [i + element.StartingRowNumber] + value
# TypeError: can only concatenate list (not "str") to list
pass
你对这些生物进行迭代并建立一个新列表的想法是正确的。只是不要建立那个奇怪的字符串
data = []
for creature in listCreatures:
data.append([creature["name"], creature["collective"]])
这很有效。作为一个新用户,我的投票没有显示出来,所以这是我唯一可以说问题已经得到完全回答的方法。