2017-04-15 49 views
0

試圖將reviews數組作爲列表打印到我的標籤中,因此它不僅僅出現在一行中。將打印列表粘貼到Tkinter標籤

import pandas as pd 
import numpy as np 
from tkinter import * 
from tkinter.filedialog import askopenfilename 
from tkinter.messagebox import showwarning, showinfo 

movies = pd.read_csv('C:/Users/Admin/Python Programs/ml-latest-small/movies.csv') 
ratings = pd.read_csv('C:/Users/Admin/Python Programs/ml-latest-small/ratings.csv') 

ratings.drop(['timestamp'], axis=1, inplace= True) 

class App(Frame): 

    def replace_name(x): 
     return movies[movies['movieId']==x].title.values[0] 

    ratings.movieId = ratings.movieId.map(replace_name) 

    M = ratings.pivot_table(index=['userId'], columns=['movieId'], values='rating') 

    def pearsons(s1, s2): 
     s1_c = s1 - s1.mean() 
     s2_c = s2 - s2.mean() 
     return np.sum(s1_c * s2_c)/np.sqrt(np.sum(s1_c ** 2) * np.sum(s2_c ** 2)) 

    def get_recs(self): 
     movie_name = self.mn.get() 
     num_recs_str = self.nr.get() 
     num_recs = int(num_recs_str) 
     reviews = [] 
     for title in App.M.columns: 
      if title==movie_name: 
       continue 
      cor = App.pearsons(App.M[movie_name], App.M[title]) 
      if np.isnan(cor): 
       continue 
      else: 
       reviews.append((title, cor)) 

     reviews.sort(key=lambda tup: tup[1], reverse=True) 
     for x in reviews: 
      self.label3.config(text= "\n" + str(reviews[:num_recs])) 
     #self.label3.config(text=reviews[:num_recs]) 
     return reviews[:num_recs] 

    def __init__(self, master): 

     Frame.__init__(self, master) 
     self.filename = None 
     label1=Label(master, text="Movie: ").grid(row=0) 
     label2=Label(master, text="Recommendations: ").grid(row=1) 
     self.label3=Label(master, text = "", font = 'Purisa', fg='blue') 
     self.label3.grid(row = 3) 
     self.mn = Entry(master) 
     self.mn.grid(row = 0, column = 1) 
     #self.mn.delete(0, END) 
     self.mn.insert(0, "Enter Movie Name") 
     self.nr = Entry(master) 
     self.nr.grid(row = 1, column = 1) 
     #self.nr.delete(0, END) 
     self.nr.insert(0, "Enter Number of Recommendations") 
     button1 = Button(self, text="Start", command=self.get_recs) 
     button2 = Button(self, text="Exit", command=master.destroy) 
     button1.grid(row = 2, padx= 5) 
     button2.grid(row = 2, column = 1, padx = 5) 
     self.grid() 

root = Tk() 
root.title("Recommender") 
root.geometry("500x500") 
app = App(root) 
root.mainloop() 

下面是電流輸出

+0

介紹一個可重複使用的簡短例子嗎? – abccd

回答

1

我無法修改代碼,因爲我沒有在你的代碼打開的文件的圖像。然而,這是一個方法來解決你的問題:

from tkinter import * 
things_to_print=['something','one thing','another'] 
root=Tk() 
for i in range(len(things_to_print)): 
    exec('Label%d=Label(root,text="%s")\nLabel%d.pack()' % (i,things_to_print[i],i)) 
root.mainloop() 

我想你可以通過替代解決您的問題,您reviews到我things_to_print,也許其他一些細微的修改。

+0

非常感謝你,經過一些小調整,這很好。生成的代碼行如下所示: '對於範圍內的(num_recs): exec('Label%d = Label(root,text =「%s」)\ nLabel%d.grid()'% 我,評論[我],我))' – Keegan

+0

@基根我很高興知道它運作良好。如果您認爲我的回答有幫助,您可以選擇接受我的回答(只需點擊此答案左側的複選符號)。 – pkqxdd