我正在读取一些csv数据文件,并试图显示数据。 如果按行读取它们,则可以按行显示数据到wx.TextCtrl。 如果我以列表形式读取数据:
exampleFile = open('example.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader)
print (exampleData)
self.logger.AppendText(exampleData)
打印效果很好,但出现以下错误:
TypeError: TextEntry.AppendText(): argument 1 has unexpected type 'list'
在wxPython中显示列表数据的首选方式是什么?
答案 0 :(得分:3)
借助Creat a Log window in Python的@Shane答案,我为您制作了一个示例。
# -*- coding: utf-8 -*-
import wx
import threading
import time
class ExamplePanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.quote = wx.StaticText(self, label="Your Log :", pos=(10, 10))
self.logger = wx.TextCtrl(self, pos=(0,40), size=(1100,1100), style=wx.TE_MULTILINE | wx.TE_READONLY)
########################################################################
# Use a thread to start your task
########################################################################
task_thread = threading.Thread(target = self.my_task, args = ())
task_thread.setDaemon(True)
task_thread.start()
def append_txt(self,txt):
self.logger.AppendText(txt)
def my_task(self):
########################################################################
# Do your job right here and update log
########################################################################
exampleFile = open('example.csv')
exampleReader = csv.reader(exampleFile)
# exampleData = list(exampleReader)
print (exampleData)
self.logger.AppendText('CSV output : \n' )
self.logger.AppendText('\n'.join(map(str,exampleData)))
'''for i in range(100):
self.append_txt('\nNew line added(No.%s)' % (i + 1))
time.sleep(1)
'''
def sample_Window():
app = wx.App(False)
frame = wx.Frame(None)
panel = ExamplePanel(frame)
frame.Show()
app.MainLoop()
sample_Window()
您对代码的具体回答是:
exampleFile = open('example.csv')
exampleReader = csv.reader(exampleFile)
# exampleData = list(exampleReader)
print (exampleData)
self.logger.AppendText('CSV output : \n' )
self.logger.AppendText('\n'.join(map(str,exampleData)))
我的示例csv内容是:
Col1,Col2
1,2
M,N
干杯。