Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Beginnings of a fixed-width file import wizard in wxPython

A draft, saved for future reference, of an analog of Python-DSV, but for fixed-length files...

from wxPython.grid import wxGrid
from wxPython.wx import wxPySimpleApp
from wxPython.wx import wxFrame
import wx
import string
import types

class FixLenDiscovery(wxFrame):
    colNum = 2
    
    rowNum = 6
    
    defLen = "5"

    def addField(self,event):
        self.fieldGrid.InsertCols(pos = self.colNum)
        self.grid.InsertCols(pos = self.colNum)
        self.fieldGrid.SetCellValue(0, self.colNum, self.defLen)
        self.colNum = self.colNum + 1
        self.Layout()
        
    def removeField(self,event):
        if self.colNum > 1:
            self.fieldGrid.DeleteCols(self.colNum  - 1)
            self.grid.DeleteCols(self.colNum  - 1)
            self.colNum = self.colNum - 1
        self.Layout()

        
    def loadFile(self,event):
         path = self.filePicker.GetPath()
         file = open(path, 'r')
         
         self.grid.ClearGrid()
         for row in range(0, self.rowNum -1):
             for col in range(0, self.colNum):
                 width = self.fieldGrid.GetCellValue(0, col)
                 width = string.atoi(width)
                 value = file.read(width)
                 self.grid.SetCellValue(row, col, value)
             
         self.grid.AutoSize()
   
    def __init__(self):
        wxFrame.__init__(self, None, -1, '', size=(1000, 500))
        
        self.filePicker = wx.FilePickerCtrl(self, 1)
        
        self.loadButton = wx.Button(self, wx.FIXED, "Preview")
        self.loadButton.Bind(wx.EVT_BUTTON, self.loadFile)
        
        self.grid=wxGrid(self, -1)
        self.grid.CreateGrid(self.rowNum, self.colNum)
        self.grid.DisableCellEditControl()
       
        topBox = wx.BoxSizer(wx.HORIZONTAL)
#        topBox.Add(wx.StaticText(self, wx.FIXED, "File: "), 1)
        topBox.Add(self.filePicker, 3)
        topBox.Add(wx.StaticText(self, wx.FIXED, ""), 1)
        topBox.Add(self.loadButton, 1)
        
        fieldsBox = wx.BoxSizer(wx.VERTICAL)
        
        fieldsButtonBox = wx.BoxSizer(wx.HORIZONTAL)
        
        self.fieldGrid = wxGrid(self, -1)
        self.fieldGrid.CreateGrid(1, self.colNum)
        for i in range(0, self.colNum):
            self.fieldGrid.SetCellValue(0, i, self.defLen)
        
        
        addFieldButton = wx.Button(self, wx.FIXED, "Add field")
        addFieldButton.Bind(wx.EVT_BUTTON, self.addField)
        removeFieldButton = wx.Button(self, wx.FIXED, "Remove field")
        removeFieldButton.Bind(wx.EVT_BUTTON, self.removeField)
        
        fieldsButtonBox.Add(addFieldButton, 1)
        fieldsButtonBox.Add(removeFieldButton, 1)
        
        fieldsBox.Add(self.fieldGrid, 1)
        fieldsBox.Add(fieldsButtonBox, 1)
        
        mySizer = wx.BoxSizer(wx.VERTICAL)
        mySizer.Add(topBox, 1)
        mySizer.Add(fieldsBox, 3)

        mySizer.Add(self.grid, 4)
        
        self.SetAutoLayout(True)
        self.SetSizer(mySizer)
        self.Layout()
       
if __name__ == "__main__":
    app = wxPySimpleApp()
    frame = FixLenDiscovery()
    frame.Show(True)
    app.MainLoop()

Python - Mouse Capture

// Minimo Esempio di pannello con evento

import wx

class MyFrame(wx.Frame):
    
    def __init__(self):
        
        # creo un frame
        wx.Frame.__init__(self, None, -1, 'My Frame', size=(300, 300))
        # aggiungo un pannello
        panel = wx.Panel(self, -1)
        # aggiungo un evento al pannello
        panel.Bind(wx.EVT_MOTION, self.OnMove)
        # aggiungo un controllo di testo
        self.posCtrl = wx.TextCtrl(panel, -1, 'Pos: ', pos=(40, 10))
        
    def OnMove(self, event):
        
        # catturo la posizione del mouse
        pos = event.GetPosition()
        # scrivo tale posizione nel controllo di testo
        self.posCtrl.SetValue('%s, %s' % (pos.x, pos.y))
        
if '__main__' == __name__:
    
    app = wx.PySimpleApp()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS