我有这个清单:
runner.sublist = [["one","two","three"],["red","black","blue]]
和这个类定义
class popupWindow(object):
def __init__(self, master, txt):
top = self.top = Toplevel(master)
self.l = Label(top, text=txt)
self.l.pack()
self.b = Button(top, text='Fine....', command=self.cleanup)
self.b.pack()
def popup(self,txt):
self.w=popupWindow(self.master, txt)
self.master.wait_window(self.w.top)
我正试图让这个按钮创建一个弹出窗口,弹出窗口中弹出runner.sublist
列表,每组元素后面都有换行符(第一行一两三,红黑蓝)第二,等等。)
def print_it(op):
out = '\n'.join(op)
return out
class mainWindow(object):
def __init__(self,master):
self.master=master
self.b2=Button(master,text="print value",command=lambda: self.popup(print_it(runner.sublist)))
self.b2.pack()
但是,此代码返回以下错误:
TypeError: sequence item 0: expected string, list found
显然,我正在传递一个列表,我应该有一个字符串,但我完全不知道为什么它会得到一个列表!我试图在不同的地方强制将一些值强制成字符串,但这没有运气。
有什么想法吗?谢谢!
答案 0 :(得分:1)
join
可以加入list of strings
,但不能加入list of lists of strings
。
(见:lambda: self.popup(print_it(runner.sublist))
)
'\n'.join( [["one","two","three"],["red","black","blue"]] ) # error
您必须更改print_it()
。例如:
def print_it(op):
return '\n'.join( ' '.join(line) for line in op )
获得两行
one two three
red black blue