【问题标题】:Python/wxPython: Importing CSV file onto wxGrid not displaying to fit on framePython/wxPython:将 CSV 文件导入 wxGrid 不显示以适合框架
【发布时间】:2014-06-18 18:34:42
【问题描述】:

我正在做一个项目,我必须将 CSV 文件导入 wx.Grid 以进行进一步操作。 我在这里搜索并找到了一个有用的方法http://wxpython-users.1045709.n5.nabble.com/new-to-the-list-opening-a-text-file-in-a-grid-using-splitterwindows-td2373808.html

现在我的 CSV 似乎根本不会显示在我的屏幕框架上(根据下面的代码)?有什么办法吗?

csv1.py 是图形用户界面

class MyFrame3 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 900,600 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        Sizer1 = wx.BoxSizer( wx.HORIZONTAL )

        Sizer1.SetMinSize( wx.Size( 0,0 ) ) 
        self.Right_Panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        RightSizer = wx.BoxSizer( wx.VERTICAL )


        self.Right_Panel.SetSizer( RightSizer )
        self.Right_Panel.Layout()
        RightSizer.Fit( self.Right_Panel )
        Sizer1.Add( self.Right_Panel, 1, wx.EXPAND |wx.ALL, 5 )

        self.Left_Panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        LeftSizer = wx.BoxSizer( wx.VERTICAL )

        self.ImportButton = wx.Button( self.Left_Panel, wx.ID_ANY, u"Import CSV File", wx.DefaultPosition, wx.DefaultSize, 0 )
        LeftSizer.Add( self.ImportButton, 0, wx.ALL, 5 )


        self.Left_Panel.SetSizer( LeftSizer )
        self.Left_Panel.Layout()
        LeftSizer.Fit( self.Left_Panel )
        Sizer1.Add( self.Left_Panel, 0, wx.EXPAND |wx.ALL, 5 )


        self.SetSizer( Sizer1 )
        self.Layout()
        self.menubar = wx.MenuBar( 0 )
        self.fileMenu = wx.Menu()
        self.importMenu = wx.MenuItem( self.fileMenu, wx.ID_ANY, u"Import", wx.EmptyString, wx.ITEM_NORMAL )
        self.fileMenu.AppendItem( self.importMenu )

        self.menubar.Append( self.fileMenu, u"&File" ) 

        self.SetMenuBar( self.menubar )


        self.Centre( wx.BOTH )

        # Connect Events
        self.ImportButton.Bind( wx.EVT_BUTTON, self.ImportFunc )
        self.Bind( wx.EVT_MENU, self.ImportFunc, id = self.importMenu.GetId() )



class csv_view(wx.App): 
        def OnInit(self): 
                self.frame=MyFrame3(None, -1, 'PyStereo', size=(900,600)) 
                self.SetTopWindow(self.frame) 
                return True 

csv2.py是运行脚本

#!/usr/bin/python
# -*- coding: utf-8 -*- 

import wx
import os
import numpy as np
import sys, csv
import wx.grid
from csv1 import MyFrame3, csv_view




class MyFrame(MyFrame3):
    def __init__(self, parent, size = wx.Size(900,600)):
        MyFrame3.__init__ (self, parent)

        self.dirname = os.getcwd()



    # Import/Open CSV

    def ImportFunc( self, event ):
        '''THIS IMPORTED CSV WILL NEVER EXPAND TO FIT INTO THE FRAME, PLEASE HELP?'''   

        dlg=wx.FileDialog(self, 'Choose a file', self.dirname, '','CSV files (*.csv)|*.csv|All files(*.*)|*.*',wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.dirname=dlg.GetDirectory()
            self.filename=os.path.join(self.dirname,dlg.GetFilename())
            self.file=file(self.filename, 'r')

            #check for file format with sniffer
            dialect = csv.Sniffer().sniff(self.file.read(1024))
            self.file.seek(0)

            csvfile=csv.reader(self.file,dialect)
            filedata = [] #put contents of csvfile into a list
            filedata.extend(csvfile)
            self.file.seek(0)

            #grab a sample and see if there is a header
            sample=self.file.read(2048)
            self.file.seek(0)
            if csv.Sniffer().has_header(sample): #if there is a header
                colnames=csvfile.next() # label columns from first line
                datalist=[] # create a list without the header
                datalist.extend(filedata[1:len(filedata)]) #append data without header

            else:
                row1=csvfile.next() #if there is NO header
                colnames=[]
                for i in range(len(row1)):
                    colnames.append('col_%d' % i) # label columns as col_1, col_2, etc
                self.file.seek(0)
                datalist=filedata #append data to datalist

        self.file.close()
        self.createGrid(datalist, colnames)



    #create the grid

    def createGrid(self, datalist, colnames):
        if getattr(self, 'grid', 0): self.grid.Destroy()
        self.grid=wx.grid.Grid(self, 0)
        self.grid.CreateGrid(len(datalist), len(colnames)) #create grid, same size as file (rows, cols)

        #fill in headings
        for i in range(len(colnames)):
            self.grid.SetColLabelValue(i, colnames[i])

        #populate the grid
        for row in range(len(datalist)):
            for col in range(len(colnames)):
                try: 
                    self.grid.SetCellValue(row,col,datalist[row][col])
                except: 
                    pass


        self.grid.AutoSizeColumns(False) # size columns to data (from cvsomatic.py)
        self.twiddle()

    def twiddle(self): # from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html
        x,y = self.GetSize()
        self.SetSize((x, y+1))
        self.SetSize((x,y))

    def Exit(self, event):
        if getattr(self, 'file',0):
            self.file.close()
            self.Close(True)

# class csv_view(wx.App):
#   def OnInit(self):
#       self.frame=MyFrame(None, -1, 'show CSV', size=(900,600))
#       self.SetTopWindow(self.frame)
#       return True


# app=csv_view() 
# app.MainLoop() 



app = wx.App(0)
Frame_02 = MyFrame(None)
Frame_02.Show()
app.MainLoop()

提前感谢您的帮助。

【问题讨论】:

    标签: python csv wxpython


    【解决方案1】:

    最大的问题是你有养育问题。创建网格小部件时,会将其添加到框架中,而不是添加到 sizer 中。这会导致网格被初始化为小尺寸,并堆叠在面板顶部。要解决此问题,您需要将网格的父级设置为其中一个面板,将网格添加到 sizer,然后在面板上调用 Layout。以下是 csv2.py 的更新代码:

    import wx
    import os
    import sys, csv
    import wx.grid
    from csv1 import MyFrame3
    
    class MyFrame(MyFrame3):
        def __init__(self, parent, size = wx.Size(900,600)):
            MyFrame3.__init__ (self, parent)
    
            self.dirname = os.getcwd()
    
    
    
        # Import/Open CSV
    
        def ImportFunc( self, event ):
            '''THIS IMPORTED CSV WILL NEVER EXPAND TO FIT INTO THE FRAME, PLEASE HELP?'''   
    
            dlg=wx.FileDialog(self, 'Choose a file', self.dirname, '','CSV files (*.csv)|*.csv|All files(*.*)|*.*',wx.OPEN)
            if dlg.ShowModal() == wx.ID_OK:
                self.dirname=dlg.GetDirectory()
                self.filename=os.path.join(self.dirname,dlg.GetFilename())
                self.file=file(self.filename, 'r')
    
                #check for file format with sniffer
                dialect = csv.Sniffer().sniff(self.file.read(1024))
                self.file.seek(0)
    
                csvfile=csv.reader(self.file,dialect)
                filedata = [] #put contents of csvfile into a list
                filedata.extend(csvfile)
                self.file.seek(0)
    
                #grab a sample and see if there is a header
                sample=self.file.read(2048)
                self.file.seek(0)
                if csv.Sniffer().has_header(sample): #if there is a header
                    colnames=csvfile.next() # label columns from first line
                    datalist=[] # create a list without the header
                    datalist.extend(filedata[1:len(filedata)]) #append data without header
    
                else:
                    row1=csvfile.next() #if there is NO header
                    colnames=[]
                    for i in range(len(row1)):
                        colnames.append('col_%d' % i) # label columns as col_1, col_2, etc
                    self.file.seek(0)
                    datalist=filedata #append data to datalist
    
            self.file.close()
            self.createGrid(datalist, colnames)
            grid_sizer = wx.BoxSizer(wx.VERTICAL)
            grid_sizer.Add(self.grid, 1, wx.EXPAND)
            self.Right_Panel.SetSizer(grid_sizer)
            self.Right_Panel.Layout()
    
    
    
        #create the grid
    
        def createGrid(self, datalist, colnames):
            if getattr(self, 'grid', 0): self.grid.Destroy()
            self.grid=wx.grid.Grid(self.Right_Panel, 0)
            self.grid.CreateGrid(len(datalist), len(colnames)) #create grid, same size as file (rows, cols)
    
            #fill in headings
            for i in range(len(colnames)):
                self.grid.SetColLabelValue(i, colnames[i])
    
            #populate the grid
            for row in range(len(datalist)):
                for col in range(len(colnames)):
                    try: 
                        self.grid.SetCellValue(row,col,datalist[row][col])
                    except: 
                        pass
    
    
            self.grid.AutoSizeColumns(False) # size columns to data (from cvsomatic.py)
            self.twiddle()
    
        def twiddle(self): # from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html
            x,y = self.GetSize()
            self.SetSize((x, y+1))
            self.SetSize((x,y))
    
        def Exit(self, event):
            if getattr(self, 'file',0):
                self.file.close()
                self.Close(True)
    
    import wx.lib.mixins.inspection
    app = wx.App(0)
    Frame_02 = MyFrame(None)
    Frame_02.Show()
    wx.lib.inspection.InspectionTool().Show()
    app.MainLoop()
    

    我还在您的代码中添加了小部件检查工具,以帮助我确定面板的布局方式以及网格的放置位置。它对于找出小部件布局的问题非常有用。你可以在 wxPython wiki 上阅读更多关于这个方便工具的信息:

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    相关资源
    最近更新 更多