lundi 6 novembre 2023

How to Compare a few Lists[], and print the Lists that have matching items

Basically Im trying to create a Random Movie Picker from a list of My Favorite films based off of a keyword input().

I want to search "Comedy", get back a list of the Comedy Titles in a textBox, as well as, One randomly selected "Comedy Title" configured into a Label.

My approach is...

list= [["Title1", "Comedy", "Movie"],
       ["Title2", "Comedy", "Movie"],
       ["Title3", "Anime", "Movie"],
       ["Title4", "Horror", "Show"],
       ["Title5", "Comedy", "Show"],
       ["Title6", "Comedy", "Show"],
       ["Title7", "Horror", "Movie"],
       ["Title8", "Comedy", "Movie"],
       ["Title9", "Comedy", "Show"],
       ["Title10", "Anime", "Show"]] 


def picker_button():
    for item in list:
        if item[1] == textBox1.get():
            textBox2.insert(customtkinter.END, item[0] + "\n")
            label.configure(text=random.choice(item[0]))

The Goal

  1. Give an input() of "Comedy" or "Show".
  2. On command, Loop through the lists, looking for the input(keyword) in each list.
  3. insert() a list of the found "titles" into a textBox.
  4. and configure() the label to a random.choice of "title"/item[0] from list of the Comedy Titles.

The Result

  1. Takes the input() fine.
  2. Loops without Error.
  3. Successfully, insert() a list of "Comedy" related "titles"/item[0] into textBox.
  4. Failed to Get random.choice of "Title". Label Configures to a single character of the item[0]/"title" from only the last list/"title10". It changes the Label to just a: "t" or "i" or "e" or "1" --ScreenShot of GUI output when input() == "Comedy"

Question:

  1. How can I get my label to configure() to a random.choice of "title"/item[0] of the "scanned lists" that meet the "Comedy" filter.
  2. and also return the entire "Title" instead of just "t" or "i"???

Heres the actual code:

from random import choice
import customtkinter
from tkinter import *
import random
from PIL import Image, ImageTk
import tkinter

# theme and color
customtkinter.set_appearance_mode("dark")  #"system", "light", or "dark"
customtkinter.set_default_color_theme("dark-blue") #"blue", "dark-blue", or "green"

#root = Tk()
root = customtkinter.CTk()
root.geometry("600x600")
root.iconbitmap('images/codemy.ico')
root.title("Bucket Pull")

img1=ImageTk.PhotoImage(Image.open("sasa.jpg"), Image.Resampling.LANCZOS)
World=customtkinter.CTkLabel(master=root, image=img1, corner_radius=15)
World.pack(pady=20, padx=20)

Logframe = customtkinter.CTkFrame(master=World, width=320, height=360)
Logframe.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)



# Movies
movies = [["Hitchhikers Guide to the Galaxy", "Comedy", "Movie"],
          ["Monty Python: Life of Brian", "Comedy", "Movie"],
          ["Nothing But Trouble", "Comedy", "Movie"],
          ["Planet Terror", "Action", "Movie"],
          ["Futurama", "Comedy", "Show"],
          ["Death Proof", "Suspense", "Movie"],
          ["The Machine", "Comedy", "Movie"],
          ["Spirited Away", "Anime", "Movie"],
          ["I Think You Should Leave!", "Comedy", "Show"],
          ["Kill Tony", "Comedy", "Show"],
          ["Seinfeld", "Comedy", "Show"],
          ["Barbie", "Comedy", "Movie"],
          ["Back to the Future", "Comedy", "Movie"],
          ["South Park", "Comedy", "Show"],
          ["Fruits Basket", "Anime", "Show"]]

def moodie():
    for item in movies:
        if item[1] == My_Mood.get():
            Mood_text.insert(customtkinter.END, item[0] + "\n")
            Mood_Label.configure(text=random.choice(item))
            
def clear():
    Mood_Label.configure()
    
Main_Label = customtkinter.CTkLabel(master=Logframe,text=" Bucket Pull ",font=("klee", 45),text_color="lightblue")
Main_Label.pack(pady=20, padx=20)
#Entry
My_Mood = customtkinter.CTkEntry(master=Logframe, width=200, height=40)
My_Mood.pack(pady=20, padx=20)
#Button
Mood_Button = customtkinter.CTkButton(master=Logframe, text="Mood", width=60, height=20, command=moodie)
Mood_Button.pack(pady=10, padx=10)
#Label
Mood_Label = customtkinter.CTkLabel(master=Logframe, text="A", font=("muna", 18),fg_color="transparent")
Mood_Label.pack(pady=20, padx=20)
#Textbox
Mood_text = customtkinter.CTkTextbox(master=Logframe, height=200, width=250)
Mood_text.pack(pady=20, padx=20)

#ADD Ween Button
        
        
        
root.mainloop()



Aucun commentaire:

Enregistrer un commentaire