Python教程

wxpython 表格控件

本文主要是介绍wxpython 表格控件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# -*- coding: utf-8 -*-

import wx
import wx.xrc
import wx.grid
import pandas as pd
from docx import Document

class MyFrame1 ( wx.Frame ):

	def __init__( self, parent ):
		wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"费用系统", pos = wx.DefaultPosition, size = wx.Size( 665,806 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

		self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )

		self.m_toolBar1 = self.CreateToolBar( wx.TB_HORIZONTAL, wx.ID_ANY )
		self.m_button1 = wx.Button( self.m_toolBar1, wx.ID_ANY, u"添加费用项", wx.DefaultPosition, wx.DefaultSize, 0 )
		self.m_toolBar1.AddControl( self.m_button1 )
		self.m_button2 = wx.Button(self.m_toolBar1, wx.ID_ANY, u"删除费用项", wx.DefaultPosition, wx.DefaultSize, 0)
		self.m_toolBar1.AddControl(self.m_button2)
		self.m_button3 = wx.Button(self.m_toolBar1, wx.ID_ANY, u"保存", wx.DefaultPosition, wx.DefaultSize, 0)
		self.m_toolBar1.AddControl(self.m_button3)
		m_comboBox3Choices = [row for row in list(self.datas()['费用项']) if type(row) != float]
		self.m_comboBox3 = wx.ComboBox(self.m_toolBar1, wx.ID_ANY, u"选择费用项", wx.DefaultPosition, wx.DefaultSize,
									   m_comboBox3Choices, 0)
		self.m_toolBar1.AddControl(self.m_comboBox3)
		m_choice1Choices = ["USD","CNY"]
		self.m_choice1 = wx.Choice(self.m_toolBar1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, m_choice1Choices, 0)
		self.m_choice1.SetSelection(0)
		self.m_toolBar1.AddControl(self.m_choice1)
		self.m_toolBar1.Realize()

		bSizer2 = wx.BoxSizer( wx.VERTICAL )

		self.m_grid2 = wx.grid.Grid( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )

		# Grid
		self.m_grid2.CreateGrid( 0, 5 )
		self.m_grid2.EnableEditing( True )
		self.m_grid2.EnableGridLines( True )
		self.m_grid2.EnableDragGridSize( False )
		self.m_grid2.SetMargins( 80, 806 )
		cols = ["费用项\n(Item)","单价\n(Unit Price)","数量\n(QTY)","币种\n(Currency)","金额\n(Amount)"]
		for i in range(5):
			self.m_grid2.SetColLabelValue(i,cols[i])
		# Columns
		self.m_grid2.SetColSize(0,300)
		self.m_grid2.SetLabelBackgroundColour("GOLDENROD")
		self.m_grid2.EnableDragColMove( False )
		self.m_grid2.EnableDragColSize( True )
		self.m_grid2.SetColLabelAlignment( wx.ALIGN_CENTER, wx.ALIGN_CENTER )
		self.m_grid2.SetColLabelSize(50)

		# Rows
		self.m_grid2.EnableDragRowSize( True )
		self.m_grid2.SetRowLabelAlignment( wx.ALIGN_CENTER, wx.ALIGN_CENTER )
		self.m_grid2.SetRowLabelSize(0)

		# Label Appearance
		# Cell Defaults
		self.m_grid2.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
		bSizer2.Add( self.m_grid2, 0, wx.ALL, 5 )

		attr = wx.grid.GridCellAttr()
		attr.SetReadOnly(True)
		self.m_grid2.SetColAttr(0,attr)
		self.m_grid2.SetColAttr(3, attr)
		self.m_grid2.SetColAttr(4, attr)

		self.SetSizer( bSizer2 )
		self.Layout()

		self.Centre( wx.BOTH )

		# Connect Events
		self.m_button1.Bind( wx.EVT_BUTTON, self.add )
		self.m_button2.Bind(wx.EVT_BUTTON, self.delete)
		self.m_button3.Bind(wx.EVT_BUTTON, self.save)
		self.Bind(wx.grid.EVT_GRID_CELL_CHANGED, self.cellChanged)
		#self.m_grid2.SetCellValue()

	def __del__( self ):
		pass
	def datas(self):
		dt = pd.read_csv("Z:\\工作\\Database\\charges.csv")
		# print(reader)
		return dt
	def cellChanged(self, event):
		pos = self.m_grid2.GetGridCursorCoords()[0]
		# print(pos)
		total_amount = int(self.m_grid2.GetCellValue(pos,1))*int(self.m_grid2.GetCellValue(pos,2))
		self.m_grid2.SetCellValue(pos,4,str(total_amount))

	# Virtual event handlers, override them in your derived class
	def add( self, event ):
		self.m_grid2.AppendRows(1)
		aa = self.m_grid2.GetNumberRows()
		self.m_grid2.SetCellValue(aa-1,0,self.m_comboBox3.Value)
		self.m_grid2.SetCellValue(aa-1,3, self.m_choice1.GetStringSelection())


	def delete(self,event):
		#GetGridCursorCoords()
		pos = self.m_grid2.GetGridCursorCoords()[0]
		#print(pos)
		self.m_grid2.DeleteRows(pos,1)

	def save(self,evt):

		rows = self.m_grid2.GetNumberRows()
		cols = self.m_grid2.GetNumberCols()
		self.m_grid2.AppendRows(3)  # 添加2行
		data = []
		USD = []
		CNY = []
		for row in range(rows):
			data.append([self.m_grid2.GetCellValue(row,col) for col in range(cols)])
			if self.m_grid2.GetCellValue(row,3) == "USD":
				USD.append(self.m_grid2.GetCellValue(row,4))
			elif self.m_grid2.GetCellValue(row,3) == "CNY":
				CNY.append(self.m_grid2.GetCellValue(row,4))

		print(USD,CNY)
		# 汇总,保存
		t_USD = 0
		t_CNY =0
		for u in range(len(USD)):
			t_USD = t_USD + int(USD[u])
		for u in range(len(CNY)):
			t_CNY = t_CNY + int(CNY[u])
		data.append(["", "", "", "", ""])
		data.append(["","","Total:","USD",str(t_USD)])
		data.append(["", "", "Total:", "CNY", str(t_CNY)])
		self.m_grid2.SetCellValue(rows+1 , 2, "Total:")
		self.m_grid2.SetCellValue(rows+1 , 3, "USD:")
		self.m_grid2.SetCellValue(rows +1, 4, str(t_USD))
		self.m_grid2.SetCellValue(rows + 2, 3, "CNY:")
		self.m_grid2.SetCellValue(rows + 2, 4, str(t_CNY))

		print(tuple(data))
		# save to the word
		doc = Document(r'Z:\工作\Database\费用模版.docx')

		tables = doc.tables

		bl_table = tables[0]
		charge_table = tables[1]

		print(len(charge_table.rows))
		# charges = [["OCEAN FREIGHT", "18000", "2", "USD"], ["Local charge", "3212", "2", "CNY"]]
		charges = data
		for c in range(len(charges)-1):
			charge_table.add_row()
		print(len(charge_table.rows))
		for i, row in enumerate(charge_table.rows):
			if i > 0:
				for j in range(5):
					row.cells[j].text = charges[i - 1][j]

		doc.save(r'Z:\工作\Database\费用模版1.docx')





if __name__ == "__main__":
    app = wx.App()
    main = MyFrame1(None)
    main.Show()
    app.MainLoop()

这篇关于wxpython 表格控件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!