wxpython phoenix:如何从wx网格单元中获取浮点值并执行数学运算?

时间:2016-09-07 05:55:47

标签: python-3.x grid wxpython wxwidgets wxgrid

我正在尝试使用wx网格构建发票,我想在数量列中添加值和价格列中的值,并将其显示在行总数中。

import wx
import wx.grid as gridlib


 class MyForm(wx.Frame):
   def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY, "Invoice")
    datag = 0


    # Add a panel so it looks the correct on all platforms
    panel = wx.Panel(self, wx.ID_ANY)
    grid = gridlib.Grid(panel)
    grid.CreateGrid(25, 3)
    note_sizer = wx.BoxSizer()
    note_sizer.Add(grid, 1, wx.EXPAND)
    panel.SetSizer(note_sizer)

    # We can set the sizes of individual rows and columns
    # in pixels
    grid.SetColSize(0, 520)

    # change a couple column labels
    grid.SetColLabelValue(0, "Product")
    grid.SetColLabelValue(1, "Quantity")
    grid.SetColLabelValue(2, "Price")
    grid.SetColFormatFloat(2)

    grid.SetCellValue(24, 0, 'Total')

if __name__ == "__main__":
    app = wx.App()
    frame = MyForm().Show()
    app.MainLoop()

任何人都可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

一旦你开始想做这样的事情,那么使用自定义网格表来包含你的数据值是个好主意。换句话说,网格不是将您的值放入网格并在需要时将值恢复,而是根据需要简单地要求您的表格显示值。这样可以轻松处理任何对您的应用程序有意义的结构和数据类型的数据。 wxPython演示中有一些示例显示了实现从public class Program { public static void Main(string[] args) { var startPacket = new StartPacket(); startPacket.len = 3; startPacket.points = new List<double> { 3.14, 5, -1023.1231311 }; // create an array to send through the socket var arr = startPacket.ToArray(); // create a StartPacket from an array we received from a socket var newStartPacket = StartPacket.FromArray(arr); } } public struct StartPacket { public uint len; public List<double> points; public byte[] ToArray() { var arr = BitConverter.GetBytes(len); foreach (var point in points) { arr = arr.Concat(BitConverter.GetBytes(point)).ToArray(); } return arr; } public static StartPacket FromArray(byte[] array) { var sp = new StartPacket(); sp.len = BitConverter.ToUInt32(array, 0); sp.points = new List<double>(); for (int i = 0; i < sp.len; i++) { sp.points.Add(BitConverter.ToDouble(array, 4 + i * 8)); } return sp; } } 派生的表类的一些方法以及如何使用它们。