Keegan
Keegan

Reputation: 15

Printing list to Tkinter Label

Trying to print the reviews array as a list to my label so it doesn't just come out in a single line.

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()

Here is an image of the current output

Upvotes: 0

Views: 3507

Answers (1)

Mia
Mia

Reputation: 2676

I couldn't modify your code because I don't have the files you opened in your code. However, this is an idea to solve your question:

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()

I think you can solve your problem by substituting your reviews into my things_to_print and maybe some other slight modifications.

Upvotes: 2

Related Questions