Here is a tkinter version. The movie search is not finished but, can search by person.
As before, it takes a minute as there are several queries being done.
As before, it takes a minute as there are several queries being done.
# Do the imports from imdb import Cinemagoer import tkinter as tk from tkinter import ttk from urllib.request import urlopen class Data: ''' The Data class retrieves all information from user input Searches person and movies returns all relevant information ''' def __init__(self): self.action = Cinemagoer() def search_person(self, name): person = self.action.search_person(name.strip())[0] return person.get('name'), person.personID def get_person(self, id): person = self.action.get_person(id) jobs = [job for job in person.get('filmography').keys()] movies = [movie for movie in person['filmography'][jobs[0]]] return person.get('name'), movies def search_movies(self, title): return self.action.search_movie(title) def get_movie(self, movie_id): data = [] movie = self.action.get_movie(movie_id) return movie class Window: ''' Window class is for visual and has fields and buttons for the Controller class to use ''' def __init__(self, parent): parent.columnconfigure(0, weight=1) parent.rowconfigure(0, weight=1) parent.minsize(800,600) parent['padx'] = 5 parent['pady'] = 3 parent['bg'] = '#c0c0c0' parent.title('Movie Search') container = tk.Frame(parent, bg='#333333') container.grid(column=0, row=0, sticky='news') container.grid_columnconfigure(0, weight=3) container.grid_rowconfigure(3, weight=3) header = tk.Label(container, text='Movie Search', fg='#ffffff', bg='#333333') header['font'] = 'tk.HEADER 28 bold' header.grid(column=0, row=0, sticky='new', padx=2, pady=2) self.msg = tk.Label(container, text='Messages: ', bg='#555555', fg='#FFFFFF', anchor='w', padx=10) self.msg['font'] = 'tk.MENU 13 normal' self.msg.grid(column=0, row=1, sticky='new', padx=2, pady=2) row1 = tk.Frame(container, bg='#555555') row1.grid(column=0, row=2, sticky='new', padx=2, pady=2) for i in range(5): weight = 4 if i == 3 else 3 row1.grid_columnconfigure(i, weight=weight, uniform='cols') row2 = tk.Frame(container, bg='#555555') row2.grid(column=0, row=3, sticky='news', padx=2, pady=2) row2.grid_columnconfigure(0, weight=3, uniform='tree') row2.grid_columnconfigure(2, weight=3, uniform='tree') row2.grid_rowconfigure(0, weight=3) row3 = tk.Frame(container, bg='#555555') row3.grid(column=0, row=4, sticky='new', padx=2, pady=2) row3.grid_columnconfigure(0, weight=3) label = tk.Label(row1, text='Search by:', bg='#555555', fg='#ffffff') label['font'] = 'None 12 normal' label.grid(column=0, row=0, sticky='new', padx=2, pady=4) options = ['Actor/Actress', 'Movie Title'] self.picked = tk.StringVar() self.picked.set(options[0]) self.searchby = tk.OptionMenu(row1, self.picked, *options) self.searchby['font'] = 'None 10 normal' self.searchby.grid(column=1, row=0, sticky='new', padx=2, pady=4) self.searchby.config(border=0) label = tk.Label(row1, text='Search For:', bg='#555555', fg='#ffffff') label['font'] = 'None 12 normal' label.grid(column=2, row=0, sticky='new', padx=2, pady=4) self.entry = tk.Entry(row1) self.entry['font'] = 'None 12 normal' self.entry.grid(column=3, row=0, sticky='new', padx=2, pady=4) self.button = tk.Button(row1, text='Search') self.button.grid(column=4, row=0, sticky='new', padx=2) columns = ('Title', 'Released', 'Movie ID') self.left_tree = ttk.Treeview(row2, columns=columns, show='headings', selectmode='browse') for column in columns: if column == columns[0]: self.left_tree.column(column, minwidth=0, width=400, stretch='yes') else: self.left_tree.column(column, minwidth=0, width=100) self.left_tree.heading(column, text=column.title()) self.left_tree.grid(column=0, row=0, sticky='news', padx=2) left_scroll = tk.Scrollbar(row2, orient='vertical', command=self.left_tree.yview) self.left_tree.config(yscrollcommand=left_scroll.set) left_scroll.grid(column=1, row=0, sticky='ns', padx=2) self.right_tree = ttk.Treeview(row2) self.right_tree.grid(column=2, row=0, sticky='news', padx=2) right_scroll = tk.Scrollbar(row2, orient='vertical', command=self.right_tree.yview) self.right_tree.config(yscrollcommand=right_scroll.set) right_scroll.grid(column=3, row=0, sticky='ns', padx=2) sig = tk.Label(row3, text='my-python.org', bg='#333333', fg='#ffffff') sig['font'] = 'None 10 normal' sig.grid(column=0, row=0, sticky='new') class Controller: ''' Controller class handles all communications between the Data and Window class ''' def __init__(self, data, window): self.data = data self.window = window # Button Commands self.window.button['command'] = self.search def search(self): ''' Search method will lookup either actor or movie depending on the selection in the Window class. At the momment the search only works for finding actors and actresses ''' # Clear the treevies self.window.left_tree.delete(*self.window.left_tree.get_children()) # Get the param for either person search or movie search picked = self.window.picked.get() # Get the name of the search. person or movie name = self.window.entry.get().strip() # Set alternation colors for the treeview self.window.left_tree.tag_configure('oddrow', background='#ccceee') self.window.left_tree.tag_configure('evenrow', background='white') # Person was selected for search if picked == 'Actor/Actress': self.window.msg['text'] = f'Messages: Search results for {name.title()}' # Clear the entry field self.window.entry.delete(0, 'end') # Call the Data class search_person method person = self.data.search_person(name.strip()) # Get relevant data using the person[1] which is set to the person id data = self.data.get_person(person[1]) # Create empty list to hold the data wanted movie_list = [] # Loop through the data getting relevant information # Also checking that there is a year for the movie release # This helps in not getting any movies not yet released for movie in data[1]: if movie.get('year'): movie_list.append((movie.get('title'), movie.get('year'), movie.movieID)) elif movie.get('year') == None: year = self.data.get_movie(movie.movieID).get('year') if year: movie_list.append((movie.get('title'), year, movie.movieID)) else: pass # Loop through the movie_list and insert into the treeview with alternating colors for index, movie in enumerate(movie_list): if index % 2 == 0: self.window.left_tree.insert('', index, values=(movie[0], movie[1], movie[2]), tags=('oddrow',)) else: self.window.left_tree.insert('', index, values=(movie[0], movie[1], movie[2]), tags=('evenrow',)) else: self.window.msg['text'] = 'Message: This function is not yet complete.' self.window.entry.delete(0, 'end') if __name__ == '__main__': root = tk.Tk() img = urlopen('https://my-python.org/images/code-forum/light/light_on.png') data = img.read() image = tk.PhotoImage(data=data) root.wm_iconphoto(True, image) controller = Controller(Data(), Window(root)) root.mainloop()
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags