1
get_recs
由start
按鈕觸發。 get_recs
中的循環首先刪除所有現有標籤(但始終失敗),然後創建標籤,寫入標籤,然後將其添加到網格中。然而,每次按下start
時,現有標籤都不會被銷燬,而是會創建新標籤而不是被替換。我只能假設這意味着每次執行循環時都會創建單獨的標籤,這可以解釋爲什麼它們永遠不會被銷燬,但我不明白爲什麼。以下是代碼:無法覆蓋標籤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)
Frame3= Frame(root, bg= 'red')
Frame3.grid()
#for i in range(num_recs):
#exec("Label%d=Label(Frame3,text='', fg = 'blue')\nLabel%d.grid()" % (i,i))
#var_exists = 'Label0' in locals() or 'Label0' in globals()
for i in range(num_recs):
try:
exec("Label%d.destroy()" % (i))
except (NameError):
pass
exec("Label%d=Label(Frame3,text='', fg = 'blue')\n" % (i))
exec("Label%d.config(text=%s)" % (i,reviews[i]))
exec("Label%d.grid()" % (i))
print ("success")
#exec("print (%d)" % (i))
#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)
Frame1 = Frame(master)
Frame1.grid()
Frame2 = Frame(master)
Frame2.grid()
self.filename = None
label1=Label(Frame1, text="Movie: ").grid(row=0, sticky=W)
label2=Label(Frame1, text="Recommendations: ").grid(row=1, sticky=W)
#self.label3=Label(master, text = "", font = 'Purisa', fg='blue')
#self.label3.grid(row = 3)
self.mn = Entry(Frame1)
self.mn.grid(row = 0, column = 1, sticky=W)
#self.mn.delete(0, END)
self.mn.insert(0, "Enter Movie Name")
self.nr = Entry(Frame1)
self.nr.grid(row = 1, column = 1, sticky=W)
#self.nr.delete(0, END)
self.nr.insert(0, "Enter Number of Recommendations")
button1 = Button(Frame2, text="Start", command=self.get_recs)
button2 = Button(Frame2, text="Exit", command=master.destroy)
button1.grid(row = 0, column = 0, padx= 5, pady = 5, sticky =W)
button2.grid(row = 0, column = 1, padx = 5, pady =5, sticky =W)
self.grid()
root = Tk()
root.title("Recommender")
root.geometry("500x500")
app = App(root)
root.mainloop()
不要使用exec!這可能很方便,但不要。 – abccd
而且你基本上總是在該循環中創建的每個標籤都有一個NameError,但你不知道,因爲你通過了它 – abccd