我是wxPython的新手。我正在构建具有动态更新的网格对象的GUI。但是,每次更新时,都会在网格周围添加一些空白。
请确保更新folderPath属性,以使代码在您的PC上正常工作。
要查看网格上的空白,请填写表格并单击确定,以创建一些帐户实例。
代码后的更多详细信息:
import wx
import pandas as pd
import os
import numpy as np
import wx.grid as gridlib
class MyForm(wx.Frame):
#..................................................................................................................
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, title='Add Account Parameters')
#maximize the app
self.Maximize(True)
# Create attributes
self.folderPath = 'C:\\Users\\wrich\\Desktop\\Fee Engine'
# Create the main panel
self.masterPanel = wx.Panel(self, wx.ID_ANY,)
self.panel = wx.Panel(self.masterPanel, -1, pos=(40,100),size=(400, 300))
self.paramPanel = wx.Panel(self.masterPanel, -1, pos=(0,410),size=(800,300))
# Create text boxes
self.accountNumberBox = wx.TextCtrl(self.panel, -1, '')
self.ficaBox = wx.TextCtrl(self.panel, -1, '')
self.personaBox = wx.TextCtrl(self.panel, -1, '')
self.txNumBox = wx.TextCtrl(self.panel, -1, '')
self.minValBox = wx.TextCtrl(self.panel, -1, '')
self.maxValBox = wx.TextCtrl(self.panel, -1, '')
#------------------------------------------------------------------------------------------------------------------
# account parameter input panel
#------------------------------------------------------------------------------------------------------------------
# Create labels
labelOne = wx.StaticText(self.panel, -1, 'Account Number')
labelTwo = wx.StaticText(self.panel, -1, 'Fica Status')
labelThree = wx.StaticText(self.panel, -1, 'Persona')
labelFour = wx.StaticText(self.panel, -1, 'Number of Transactions')
labelFive = wx.StaticText(self.panel, -1, 'Minimum Transaction Value')
labelSix = wx.StaticText(self.panel, -1, 'Maximum Transaction Value')
# Create buttons
okBtn = wx.Button(self.panel, -1, 'OK')
cancelBtn = wx.Button(self.panel, -1, 'Cancel')
# Bind buttons to events
self.Bind(wx.EVT_BUTTON, self.onOK, okBtn)
self.Bind(wx.EVT_BUTTON, self.onCancel, cancelBtn)
# Create the sizers sizers(groups)
topSizer = wx.BoxSizer(wx.VERTICAL)
self.botSizer = wx.BoxSizer(wx.VERTICAL)
titleSizer = wx.BoxSizer(wx.HORIZONTAL)
gridSizer = wx.GridSizer(rows=6, cols=2, hgap=5, vgap=5)
inputOneSizer = wx.BoxSizer(wx.HORIZONTAL)
inputTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
inputThreeSizer = wx.BoxSizer(wx.HORIZONTAL)
inputFourSizer = wx.BoxSizer(wx.HORIZONTAL)
inputFiveSizer = wx.BoxSizer(wx.HORIZONTAL)
inputSixSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
# each input sizer will contain a label and a sizer
inputOneSizer.Add((20,-1), proportion=1) # this is a spacer
inputOneSizer.Add(labelOne, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
inputTwoSizer.Add((20,20), 1, wx.EXPAND) # this is a spacer
inputTwoSizer.Add(labelTwo, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
inputThreeSizer.Add((20,20), 1, wx.EXPAND) # this is a spacer
inputThreeSizer.Add(labelThree, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
inputFourSizer.Add((20,20), 1, wx.EXPAND) # this is a spacer
inputFourSizer.Add(labelFour, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
inputFiveSizer.Add((20,20), 1, wx.EXPAND) # this is a spacer
inputFiveSizer.Add(labelFive, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
inputSixSizer.Add((20,20), 1, wx.EXPAND) # this is a spacer
inputSixSizer.Add(labelSix, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
# Right align the labels and icons
# Set the TextCtrl to expand on resize
gridSizer.Add(inputOneSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.accountNumberBox, 0, wx.EXPAND)
gridSizer.Add(inputTwoSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.ficaBox, 0, wx.EXPAND)
gridSizer.Add(inputThreeSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.personaBox, 0, wx.EXPAND)
gridSizer.Add(inputFourSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.txNumBox, 0, wx.EXPAND)
gridSizer.Add(inputFiveSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.minValBox, 0, wx.EXPAND)
gridSizer.Add(inputSixSizer, 0, wx.ALIGN_RIGHT)
gridSizer.Add(self.maxValBox, 0, wx.EXPAND)
btnSizer.Add(okBtn, 0, wx.ALL, 5)
btnSizer.Add(cancelBtn, 0, wx.ALL, 5)
topSizer.Add(titleSizer, 0, wx.CENTER)
topSizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(gridSizer, 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(wx.StaticLine(self.panel), 0, wx.ALL|wx.EXPAND, 5)
topSizer.Add(btnSizer, 0, wx.ALL|wx.CENTER, 5)
# SetSizeHints(minW, minH, maxW, maxH)
self.SetSizeHints(1200,800,5000,4000)
self.panel.SetSizer(topSizer)
topSizer.Fit(self)
self.panel.Layout()
#------------------------------------------------------------------------------------------------------------------
# parameter table panel
#------------------------------------------------------------------------------------------------------------------
#get the design parameters of the grid
if os.path.isfile(str(self.folderPath) + '\\Account Parameters.xlsx'):
#get the existing parameters
accountParams = pd.read_excel(str(self.folderPath) + '\\Account Parameters.xlsx')
numRows = len(accountParams)
else:
numRows = 1
#create the grid object
self.myGrid = gridlib.Grid(self.paramPanel)
self.myGrid.CreateGrid(numRows, 6)
#size the grid
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.myGrid, 1, wx.EXPAND)
self.paramPanel.SetSizer(sizer)
#set the values of the grid
for row in range(len(accountParams)):
for col in range(6):
val = str(accountParams.loc[row][col])
self.myGrid.SetCellValue(row, col, val)
#change the headers of the table
self.myGrid.SetColLabelValue(0, "Account Number")
self.myGrid.SetColLabelValue(1, "Fica Status")
self.myGrid.SetColLabelValue(2, "Persona")
self.myGrid.SetColLabelValue(3, "# Txns")
self.myGrid.SetColLabelValue(4, "Min Txn Value")
self.myGrid.SetColLabelValue(5, "Max Txn Value")
self.myGrid.SetRowLabelSize(30)
self.myGrid.SetDefaultColSize(100, True)
#final formatting
self.botSizer.Add(self.myGrid, 0 , wx.CENTER)
self.paramPanel.SetSizer(self.botSizer)
self.botSizer.Fit(self)
self.paramPanel.Layout()
#..................................................................................................................
def onOK(self, event):
"""
Take the values and append to the account parameters
"""
#------------------------------------------------------------------------------------------------------------------
#Check that the values are not blank
if (str(self.accountNumberBox.GetValue()) == "") or (str(self.ficaBox.GetValue()) == "") or (str(self.personaBox.GetValue()) == "") or (str(self.txNumBox.GetValue()) == "") or (str(self.minValBox.GetValue()) == "") or (str(self.maxValBox.GetValue()) == ""):
print('A value was not entered')
return ()
#------------------------------------------------------------------------------------------------------------------
#check if the account parameters exist
if os.path.isfile(str(self.folderPath) + '\\Account Parameters.xlsx'):
#get the existing parameters
accountParams = pd.read_excel(str(self.folderPath) + '\\Account Parameters.xlsx')
#filter for the account
hasHist = accountParams[accountParams['Account Number'].astype(str) == str(self.accountNumberBox.GetValue())]
#decide if the account number is already there
if len(hasHist) > 0:
print('The account already exists. Overwriting the account.')
#remove the row from the data
accountParams = accountParams[accountParams['Account Number'].astype(str) != str(self.accountNumberBox.GetValue())]
else:
print('the account does not exist in the data yet')
#if the parameters dont exist yet create a new data frame
else:
accountParams = pd.DataFrame(np.random.randint(low=0, high=10, size=(0, 6)),columns=['Account Number', 'Fica Status', 'Persona', 'Number of Transactions', 'Min Tx Amount', 'Max Tx Amount'])
#------------------------------------------------------------------------------------------------------------------
#append the row to the data set
accountParams.loc[len(accountParams)] = [
int(self.accountNumberBox.GetValue()),
str(self.ficaBox.GetValue()),
str(self.personaBox.GetValue()),
int(self.txNumBox.GetValue()),
int(self.minValBox.GetValue()),
int(self.maxValBox.GetValue())
]
#write to excel
accountParams.to_excel(str(self.folderPath) + '\\Account Parameters.xlsx', index = False, header = True)
print('Successfully added the data to excel')
#------------------------------------------------------------------------------------------------------------------
#refresh the table
#get the existing parameters
accountParams = pd.read_excel(str(self.folderPath) + '\\Account Parameters.xlsx')
numRows = len(accountParams)
#decide if we need to add a row
if numRows != int(self.myGrid.GetNumberRows()):
print(str(numRows) + str(self.myGrid.GetNumberRows()))
self.myGrid.AppendRows(1, updateLabels = True)
print('added a row to the grid!')
#set the values of the grid
for row in range(len(accountParams)):
for col in range(6):
val = str(accountParams.loc[row][col])
self.myGrid.SetCellValue(row, col, val)
self.myGrid.ForceRefresh()
#..................................................................................................................
def onCancel(self, event):
self.Close()
#..................................................................................................................
def closeProgram(self):
self.Close()
# Run the program
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()
del app
因此,如果您看到我的屏幕截图,则网格右下角附近会有空白。如何删除它?
我又如何查看整个网格?我想避免使用滑块查看网格数据。This link是这些问题的图片
谢谢大家!