2014-06-19 41 views
0

我有這個名單:在使用TKinter打印用換行列表彈出

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 

很顯然,我路過一個名單,我應該有一個字符串,但我完全難住了,爲什麼它變得列表!我試圖在不同的地方將一些值強制轉換爲字符串,但那沒有運氣。

任何想法?謝謝!

回答

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 
+0

幹得好,先生 – testname123