按下按钮事件时如何使用新的wx.panel或wx.ListBox

时间:2018-07-13 07:46:06

标签: python csv wxpython

我是python新手,请尝试创建一个应用程序来处理来自不同CSV文件的数据。我的应用程序始终会处理第一个打开的CSV文件数据,但我不知道代码哪里出了错?以下是我的示例:

Sample1.csv:

ABC,DEF,GHI

1,3,7

2,5,8

3,6,9

Sample2.csv:

JKL,MNO,PQR,STU

1、3、7、10

2,5,8,11

3,6,9,12

我的应用程序将打开CSV文件,并将CSV文件的标头(如ABC,DEF,GHI)放入wx.Listbox选择中,然后单击选择的内容进行处理。首先打开sample1.csv看起来不错,但是当我打开sample2.csv并单击JKL,MNO,PRQ和STU时,面板将返回sample1.csv数据! 不知道为什么需要清除旧面板然后创建新面板吗?如果是,该怎么办?还是我需要创建一个新框架来处理csv数据?

感谢您的帮助!

import csv
import wx 

class Mywin(wx.Frame): 

def __init__(self, parent, title): 

    super(Mywin, self).__init__(parent, title = title,size = (550,500)) 

    toolbar = self.CreateToolBar()
    OpenCsvBtn = wx.Button(toolbar, -1," Open CSV")
    self.Bind(wx.EVT_BUTTON, self.OnClick, OpenCsvBtn)

    self.Centre() 
    self.Show(True)          

def onListBox(self, event): 
    self.text.AppendText( "Current selection:"+event.GetEventObject().GetStringSelection()+"\n")

def OnClick(self, event):

    panel = wx.Panel(self)

    print("open the csv file and get the path")
    frame = wx.Frame(None, -1, 'win.py')
    openFileDialog = wx.FileDialog(frame, "Open CSV file", "", "", 
                                  "CSV files (*.csv)|*.csv", 
                                   wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
    openFileDialog.ShowModal()
    csvPath=openFileDialog.GetPath()
    print(csvPath)
    with open(csvPath) as csvFile:
         csvReader = csv.reader(csvFile)  

         i=csvReader.next()
         rest = [row for row in csvReader]
    print (i)

    box = wx.BoxSizer(wx.HORIZONTAL) 
    self.text = wx.TextCtrl(panel,size =(200,400), style = wx.TE_MULTILINE) 
    languages = i #[title[0]]  
    lst = wx.ListBox(panel, size = (200,400), choices = languages, style = wx.LB_SINGLE)
    box.Add(lst,0,wx.EXPAND) 
    box.Add(self.text, 1, wx.EXPAND)
    panel.SetSizer(box) 
    panel.Fit() 

    self.Bind(wx.EVT_LISTBOX, self.onListBox, lst) 

ex = wx.App() 
Mywin(None,'ListBox Demo')
ex.MainLoop()

1 个答案:

答案 0 :(得分:0)

我无法理解您如何使发布的代码正常工作。它肯定不会显示工具栏。
由于您每次都创建面板,因此它无需执行任何其他操作即可工作,但您不应声明frame,因为已经创建了面板。
下面是我认为您尝试的内容的被黑客入侵的版本。
请注意,此代码适用于python 2.7,但不适用于python 3,因为工具栏的API已更改

import csv
import wx

class Mywin(wx.Frame):

    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title = title,size = (550,500))
        toolbar = self.CreateToolBar()
        open_bmp = wx.Bitmap('3.bmp')
        tool1 = toolbar.AddTool(wx.ID_ANY, open_bmp, wx.NullBitmap)
        toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.OnClick, tool1)
        self.Bind(wx.EVT_CLOSE, self.OnExit)
        self.Centre()
        self.Show(True)

    def onListBox(self, event):
        self.text.AppendText( "Current selection:"+event.GetEventObject().GetStringSelection()+"\n")

    def OnClick(self, event):
        panel = wx.Panel(self)
        print("open the csv file and get the path")
        openFileDialog = wx.FileDialog(panel, "Open CSV file", "", "",
                                      "CSV files (*.csv)|*.csv",
                                       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        csvPath=openFileDialog.GetPath()
        print(csvPath)
        with open(csvPath) as csvFile:
             csvReader = csv.reader(csvFile)

             i=csvReader.next()
             rest = [row for row in csvReader]
        print (i)

        box = wx.BoxSizer(wx.HORIZONTAL)
        self.text = wx.TextCtrl(panel,size =(200,400), style = wx.TE_MULTILINE)
        languages = i #[title[0]]
        lst = wx.ListBox(panel, size = (200,400), choices = languages, style = wx.LB_SINGLE)
        box.Add(lst,0,wx.EXPAND)
        box.Add(self.text, 1, wx.EXPAND)
        panel.SetSizer(box)
        panel.Fit()
        self.Bind(wx.EVT_LISTBOX, self.onListBox, lst)

    def OnExit(self, evt):
        self.Destroy()

ex = wx.App()
Mywin(None,'ListBox Demo')
ex.MainLoop()