英文:
How to insert a list of variable length into wx.ListCtrl or wx.ComboBox
问题
我有一个从SQL数据库中提取的数据,然后放入一个数据框中,再转换为列表。我正在创建一个GUI,允许您选择一个零件号,然后根据所选的零件号更新数据库的部分内容。然而,我在框中只得到了一个包含整个列表的单个选择。如何将列表分解为每个编号的单个选择?我已经包含了整个GUI,并进行了一些更改,以便它可以独立工作。列表的长度将发生巨大变化,因此我不能只是逐个附加每个选择。
GUI显然还没有完成或格式化正确,请在这方面多多包涵。
我想要将列表"dflist"中的每个选择都作为单独的选择。如何将列表分解为单独的条目,或者让ListCtrl将它们识别为单独的条目?
英文:
I have a data that I pulled from a SQL database, then put into a dataframe, then converted to a list. I'm creating a GUI that will let you select a part number, then update parts of the database based on what part number is selected. However, I get a single selection in the box that's the ENTIRE list. How can I break up the list into a single selection for each number? I've included the entire GUI and changed a few things so it should work standalone. The length of the list will change dramatically so I can't just individually append each selection.
The GUI is very obviously not finished or formatted correctly so please bear with me in that regard.
import wx
import wx.adv
import sqlalchemy
from six.moves import urllib
import pandas as pd
#Reads the SQL Column to use for the dropdown menu
params = urllib.parse.quote_plus("Driver={ODBC Driver 18 for SQL Server};"
"Server=SERVER;"
"Database=DB;"
"Trusted_Connection=yes;"
"TrustServerCertificate=yes"
)
engine = sqlalchemy.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
engine.connect()
#Gets list of Part Numbers for the menubox as well as matching to SQL DF
sqlread = pd.read_sql_query('''SELECT * FROM Shortage''',engine)
pndf = pd.DataFrame(sqlread, columns = ['PN'])
pndf = pndf[pndf.PN != None]
dflist = pndf.to_string(index=False,header=0)
#I'd just make your own list here to get similar results, but I want to demonstrate that I'm pulling the list from a SQL server
print(dflist)
class compwin(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Shortage Sheet Compiler v0.1',
size=(400,300))
panel = wx.Panel(self,-1)
#textbox printout
#Prompts for Update Field
wx.TextCtrl(panel,value = "Update What Part?",size=(100,20),pos=(25,20),style=wx.TE_READONLY|wx.BORDER_NONE)
#Text Boxes for Update Field
Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(125,125),pos=(20,40))
Combobox.EnableCheckBoxes()
Combobox.Append([dflist])
if __name__ == '__main__':
app = wx.App()
frame = compwin()
frame.Show()
app.MainLoop()
I want every selection in the list "dflist" to be individually selectable. How can I break the list into individual entries, or get the ListCtrl to recognize them as individual entries?
答案1
得分: 1
这是你要翻译的内容:
目前还不清楚你遇到的问题是什么,除非你使用style=wx.LC_REPORT
声明了控件,否则可能很简单。
import wx
dflist = [["123","abc"],["456","def"],["789","ghi"],["132","jkl"],["465","mno"]]
class compwin(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Shortage Sheet Compiler v0.1',
size=(400,300))
panel = wx.Panel(self,-1)
#textbox printout
#Prompts for Update Field
wx.TextCtrl(panel,value = "Update What Part?",size=(150,20),pos=(25,20),style=wx.TE_READONLY|wx.BORDER_NONE)
#Text Boxes for Update Field
Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(300, -1),pos=(25, 50), style=wx.LC_REPORT)
Combobox.InsertColumn(0, "Item", wx.LIST_FORMAT_RIGHT)
Combobox.InsertColumn(1, "Name", wx.LIST_FORMAT_RIGHT)
for item in dflist:
Combobox.Append((item[0],item[1]))
if __name__ == '__main__':
app = wx.App()
frame = compwin()
frame.Show()
app.MainLoop()
英文:
It isn't completely clear why you are having a problem, other than you have not declared the control with a style=wx.LC_REPORT
, it may be that simple.
import wx
dflist = [["123","abc"],["456","def"],["789","ghi"],["132","jkl"],["465","mno"]]
class compwin(wx.Frame):
def __init__(self):
wx.Frame.__init__(self,None,-1,'Shortage Sheet Compiler v0.1',
size=(400,300))
panel = wx.Panel(self,-1)
#textbox printout
#Prompts for Update Field
wx.TextCtrl(panel,value = "Update What Part?",size=(150,20),pos=(25,20),style=wx.TE_READONLY|wx.BORDER_NONE)
#Text Boxes for Update Field
Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(300, -1),pos=(25, 50), style=wx.LC_REPORT)
Combobox.InsertColumn(0, "Item", wx.LIST_FORMAT_RIGHT)
Combobox.InsertColumn(1, "Name", wx.LIST_FORMAT_RIGHT)
for item in dflist:
Combobox.Append((item[0],item[1]))
if __name__ == '__main__':
app = wx.App()
frame = compwin()
frame.Show()
app.MainLoop()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论