根据选定的列表框值进行推荐

时间:2019-07-24 11:54:05

标签: python tkinter

代码有点长,希望您理解它,我有这个小应用程序,在这里我必须实现2个文件中的数据,然后在列表框中显示一些(数据)值。另一方面,我将嵌套字典中的值组合在一起。现在,我必须根据将从列表框中选择的值来推荐电影。也许我编写的代码在某些部分是错误的,但是在最后一部分中,当我必须编写推荐代码时,我不知道如何编写代码,因此它将向我推荐电影,以供进一步参考解释,如果需要的话,我将在评论部分中撰写。 (数据和建议文件:https://github.com/chuanqi305/MobileNet-SSD

from Tkinter import *
import tkFileDialog
import csv
from recommendations import *
class STproject:

def __init__(self): #1

    self.variableclasses()
    self.buttonsnlabels()
    self.dictionary()


def variableclasses(self):
    self.all_vars = [StringVar() for _ in range(2)]
    self.var1 = StringVar()
    self.var2 = StringVar()

    self.var1.set(0)
    self.var2.set(0)

def buttonsnlabels(self):

    self.rdbttn=Radiobutton(root,text='user based recommendation',variable = self.var1,value=1,command=lambda:self.dictionary())#, command=lambda :self.selec1())
    self.rdbttn.grid(row=0,column=0)
    self.rdbttn2=Radiobutton(root,text='movie based recommendation',variable = self.var1,value=2,command=lambda:self.dictionary())#,command=lambda :self.selec1())
    self.rdbttn2.grid(row=1,column=0)
    self.rdbttn3=Radiobutton(root,text='pearson',variable = self.var2,value=1)#,command=lambda :self.selec2())
    self.rdbttn3.grid(row=2,column=0)
    self.rdbttn4=Radiobutton(root,text='euclidean',variable = self.var2,value=2)#,command=lambda :self.selec2())
    self.rdbttn4.grid(row=3,column=0)

    self.ratingbutton=Button(root,text='Upload Rating',command=lambda :self.file(self.all_vars[0]))
    self.ratingbutton.grid()
    self.ratingbutton=Button(root,text='Upload Movies',command=lambda :self.file(self.all_vars[1]))
    self.ratingbutton.grid()

    self.lb1 = Listbox(root)
    self.lb1.grid(row=0,column=1)
    self.lb2 = Listbox(root)
    self.lb2.grid(row=0,column=1)
    self.lb3 = Listbox(root)
    self.lb3.grid(row=0,column=1)

def file(self, v):
    result = tkFileDialog.askopenfilename()
    if result:
        v.set(result)
    # self.dictionary()

def dictionary(self):
    # self.lb1.delete(END, 0)  #clear listbox
    # self.lb1.update_idletasks()
    if self.var1.get()==1:
        self.d = {}
        if all(i.get() for i in self.all_vars): #process only if all 2 files are selected
            with open(self.all_vars[0].get(),"r") as a, open(self.all_vars[1].get(),"r") as b:
                for line1,line2,line3 in zip(csv.reader(a),csv.reader(b),csv.reader(a)):
                    self.d[line1[0]]={line2[1]:line3[2]}
                    self.lb1.insert('end',line2[1])
                print self.d

    else:
        self.d = {}
        if all(i.get() for i in self.all_vars): #process only if all 2 files are selected
            with open(self.all_vars[0].get(),"r") as a, open(self.all_vars[1].get(),"r") as b:
                for line1,line2,line3 in zip(csv.reader(a),csv.reader(b),csv.reader(a)):
                    self.d[line1[0]]={line2[1]:line3[2]}
                    self.lb1.insert('end', line1[1])
                print self.d

def recoms(self):
    # selection=self.lb1.curselection()
    # print selection
    if self.var2.get()==1:
        getRecommendations()
    else:
        pass
root=Tk()
root.title('SteelBox Inc. Calculator')
application=STproject() #2
root.mainloop() #3

0 个答案:

没有答案