使用wxPython按钮刷新ListCtrl框

问题描述 投票:0回答:1

我有一个 ListCtrl 框,它链接到我的 SQL 数据库并从中提取零件号列表,以允许用户选择更新某些零件。我想让用户刷新列表,以便他们可以查看更新后的列表。

这是我到目前为止所拥有的:

import sys
import wx
from six.moves import urllib
import wx.adv
from datetime import date
import sqlalchemy
from sqlalchemy import *
import sqlalchemy_utils 
import pandas as pd
import datetime
import logging
import os

#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=DATABASE;"
                              "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 TABLE''',engine)
pndf = pd.DataFrame(sqlread, columns = ['PN'])
pndf = pndf[pndf.PN != None]
dflist = pndf.values.tolist()


class compwin(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'',
                          size=(300,300))
        panel = wx.Panel(self,-1)
        self.part = None
        #textbox printout

        #Prompts for Update Field
        wx.TextCtrl(panel,value = "Update Part:",size=(100,20),pos=(5,10),style=wx.TE_READONLY|wx.BORDER_NONE)
        def ComboBox(panel):
            self.ComboUp = wx.ListCtrl(panel, wx.ID_ANY, size=(150,125),pos=(10,30),style=wx.LC_REPORT|wx.LC_SORT_ASCENDING)
            self.ComboUp.InsertColumn(0,"Select a Part Number:",wx.LIST_FORMAT_RIGHT)
            self.ComboUp.SetColumnWidth(0,150)
            for Part in dflist:
                self.ComboUp.Append((Part))
            return
        ComboBox(panel)

        #define buttons
        self.refresh = wx.Button(panel,label ="Refresh", name ="3",pos=(200,180))
        
        #bindings for panel
   
        self.Bind(wx.EVT_BUTTON,self.OnClick_Refresh,self.refresh)

############################ REFRESH BUTTON #########################################
    def OnClick_Refresh(self,e):
        self.ComboUp.Destroy()
        wx.BeginBusyCursor()
        params = urllib.parse.quote_plus("Driver={ODBC Driver 18 for SQL Server};"
                              "Server=SERVER;"
                              "Database=DATABASE;"
                              "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 TABLE''',engine)
        pndf = pd.DataFrame(sqlread, columns = ['PN'])
        pndf = pndf[pndf.PN != None]
        dflist = pndf.values.tolist()
        self.ComboUp = wx.ListCtrl(self,wx.ID_ANY, size=(150,125),pos=(10,10),style=wx.LC_REPORT|wx.LC_SORT_ASCENDING)
        self.ComboUp.InsertColumn(0,"Select a Part Number:",wx.LIST_FORMAT_RIGHT)
        self.ComboUp.SetColumnWidth(0,150)
        for Part in dflist:
            self.ComboUp.Append((Part))        
        wx.EndBusyCursor()

        
if __name__ == '__main__':
    app = wx.App()
    frame = compwin()
    frame.Show()
    app.MainLoop()

我尝试将其作为 def Onclick_Refresh 按钮内的嵌套函数运行

j = compwin()
j.ComboBox()

但我收到错误

AttributeError:“compwin”对象没有属性“ComboBox”。您是说:“ComboUp”吗?

如何通过单击此按钮刷新我的列表?我认为销毁窗口并重新执行 ListCtrl 的定义是最简单的方法,但我误解了一些至关重要的事情,以便使其正常工作。

python wxpython
1个回答
0
投票

避免使用问题中的代码并使用之前问题/答案中的代码,我们可以证明,您只需清除

ListCtrl
并重新填充它。

import wx

dflist = [
          ["123","Widget 1", "w101"],["456","Widget 2", "w2091"],["789","Widget 3", "w3000"],["132","Widget 4", "w404"],
          ["465","Widget X", "x555"]
         ]

dflist2 = [
          ["998","Widget 998", "w999"],["999","Widget 999", "w999"]
         ]

class compwin(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,'Shortage Sheet Compiler v0.1',
                          size=(600,300))
        panel = wx.Panel(self,-1)

        #textbox printout

        #Prompts for Update Field
        wx.TextCtrl(panel, -1, value="Update Part?", size=(150,20), pos=(25,20), style=wx.TE_READONLY|wx.BORDER_NONE)

        #Text Boxes for Update Field
        self.Combobox = wx.ListCtrl(panel, wx.ID_ANY, size=(400, -1), pos=(25, 50), style=wx.LC_REPORT)
        self.Combobox.InsertColumn(0, "Item", wx.LIST_FORMAT_RIGHT)
        self.Combobox.InsertColumn(1, "Name", wx.LIST_FORMAT_RIGHT)
        self.Combobox.InsertColumn(2, "Part Number", wx.LIST_FORMAT_RIGHT)
        self.Combobox.SetColumnWidth(1, 150)
        self.Combobox.SetColumnWidth(2, 150)

        for item in dflist:
            self.Combobox.Append((item[0], item[1], item[2]))

        #define buttons
        self.refresh = wx.Button(panel, -1, label ="Refresh", name ="3", pos=(450,180))

        #bindings for panel

        self.Bind(wx.EVT_BUTTON, self.OnClickRefresh, self.refresh)

    def OnClickRefresh(self, event):
        self.Combobox.DeleteAllItems()
        for item in dflist2:
            self.Combobox.Append((item[0], item[1], item[2]))

if __name__ == '__main__':
    app = wx.App()
    frame = compwin()
    frame.Show()
    app.MainLoop()
© www.soinside.com 2019 - 2024. All rights reserved.