定义一个dialog类,在设置必要的窗口属性后,可参照panel的布局方式进行排布。
# 自定义对话框类,新增样机信息弹窗 class DetailGridDialog(wx.Dialog): def __init__(self, parent,parameter1,parameter2,parameter3): super().__init__(parent, title="详情表单", style=wx.RESIZE_BORDER | wx.DEFAULT_DIALOG_STYLE | wx.MAXIMIZE_BOX | wx.MINIMIZE_BOX ) self.parameter1= parameter1 self.parameter2= parameter2 self.parameter3= parameter2 self.DialogGridPanel = wx.Panel(self,size = (1850,500)) DetailWriteSizer0 = wx.BoxSizer(wx.VERTICAL) #------------------------------------------------------------------- self.grid3 = wx.grid.Grid(self.DialogGridPanel) self.grid3.CreateGrid(60, 11) # 创建一个 初始200行 10 列的表格 #设置列表头 self.grid3.SetColLabelValue(0, "表头1") #设置列宽 self.grid3.SetColSize(0,60) #设置列表头 self.grid3.SetColLabelValue(1, "表头2") #设置列宽 self.grid3.SetColSize(1,300) self.grid3.SetColLabelValue(2, "表头3") self.grid3.SetColSize(2,300) self.grid3.SetColLabelValue(3, "表头4") self.grid3.SetColSize(3,300) self.grid3.SetColLabelValue(4, "表头5") self.grid3.SetColSize(4,300) self.grid3.SetColLabelValue(5, "表头6") self.grid3.SetColSize(5,240) self.grid3.SetColLabelValue(6, "表头7") self.grid3.SetColSize(6,80) self.grid3.SetColLabelValue(7, "表头8") self.grid3.SetColSize(7,80) self.grid3.SetColLabelValue(8, "表头9") self.grid3.SetColSize(8,90) self.grid3.SetColLabelValue(9, "表头10") self.grid3.SetColSize(9,50) self.grid3.SetColLabelValue(10, "") self.grid3.SetColSize(10,240) #不显示默认的行标签序号 self.grid3.HideRowLabels() #禁用编辑功能 self.grid3.EnableEditing(False) #设置表单背景颜色 self.grid3.SetDefaultCellBackgroundColour("#F5F5F5") #居中显示 self.grid3.SetDefaultCellAlignment(wx.ALIGN_CENTER, wx.ALIGN_CENTER) # 绑定列点击事件 self.grid3.Bind(gridlib.EVT_GRID_CELL_LEFT_CLICK, self.Action_write_edit) DetailWriteSizer0.Add(self.grid3,0,wx.ALL|wx.EXPAND,5) #设置内容文本格式数据,后续 font = wx.Font(11, wx.FONTFAMILY_DEFAULT, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL) RowCount = 0 self.baseINfolist = […………] #信息列表,……表示具体信息 for piceinfo in self.baseINfolist: RoWHeighList3 = [] # 设置第一行的高度为 40 self.grid3.SetRowSize(RowCount, 40) ColCount = 0 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息10'])) RoWHeighList3.append(str(piceinfo['信息10']).count("\n")) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息11'])) RoWHeighList3.append(str(piceinfo['信息11']).count("\n")) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息12'])) RoWHeighList3.append(str(piceinfo['信息12']).count("\n")) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息13'])) RoWHeighList3.append(str(piceinfo['信息13']).count("\n")) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息14'])) RoWHeighList3.append(str(piceinfo['信息14']).count("\n")) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息15'])) RoWHeighList3.append(str(piceinfo['信息15']).count("\n")) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息16'])) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['信息17'])) RoWHeighList3.append(str(piceinfo['信息17']).count("\n")) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str(piceinfo['日期'])) self.grid3.SetCellFont(RowCount, ColCount, font) ColCount += 1 self.grid3.SetCellValue(RowCount, ColCount, str('编辑')) renderer = BlueUnderlineRenderer()#这个是自定义类,参照随笔记录中表格超链接实现随笔 self.grid3.SetCellRenderer(RowCount,ColCount, renderer) # 设置特定单元格的渲染器 ColCount += 1 #取列表中“\n”最大数值 RowMaxHeigh3 = max(RoWHeighList3) if int(RowMaxHeigh3) >= 2: #设置最高行高 self.grid1.SetRowSize(RowCount, 25*(int(RowMaxHeigh3))) RowCount += 1 for col0 in range(9): self.grid3.SetCellValue(RowCount, col0, str("新增信息")) renderer = BlueUnderlineRenderer() self.grid3.SetCellRenderer(RowCount,col0, renderer) # 设置特定单元格的渲染器 # 合并第六行的所有单元格 self.grid3.SetCellSize(RowCount, 0, 1,10) self.grid3.SetRowSize(RowCount, 40) for row in range(RowCount): self.grid3.AutoSizeRow(row) # 自动适应行高 self.DialogGridPanel.SetSizer(DetailWriteSizer0) self.Fit() self.DialogGridPanel.Fit() def get_input_values(self): #这里假设有一个需要反馈的信息列表 infolist = […………] #当然,还可以通过Getvalue等方法获取对应控件的值,具体看需求 return infolist
然后我们在窗口实例化以后,调用窗口自定义类时定义的get_input_values方法,以获取窗口需要传递给父级的数据:
# 创建自定义对话框对象 detailGRIDDialog = DetailGridDialog(None,parameter1, parameter2,paramete3) #居中显示 detailGRIDDialog.Centre() # 显示对话框并获取用户的选择 result = detailGRIDDialog.ShowModal() if result == wx.ID_OK: #调用在实例中调用get_input_values()方法 writeresult = detailGRIDDialog.get_input_values()
标签:Dialog,grid3,self,RowCount,ColCount,str,dialog,弹窗,wx From: https://www.cnblogs.com/babashi9527/p/17662811.html