尝试将reviews
数组作为列表打印到我的标签中,这样它就不会出现在一行中。
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 :(得分: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
以及其他一些细微的修改来解决您的问题。