Random Movie Picker开发:如何实现符合筛选条件的完整随机标题展示?
随机影视选择器Label功能异常修复方案
问题描述
开发基于关键词输入的Random Movie Picker,预期功能:
- 输入如"Comedy"或"Show"的关键词,点击按钮遍历影视列表
- 将所有符合条件的标题插入textBox
- 在Label中展示从符合条件标题里随机选出的完整标题
当前代码可完成输入接收、筛选并插入textBox,但Label存在两个异常:
- 仅从最后一个符合条件的条目中选择内容
- 仅显示单个字符,无法展示完整标题
问题根源
问题出在moodie()函数的逻辑:
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))
- 每次循环到符合条件的条目时,都会重新覆盖Label的文本,最终只保留最后一次循环的设置
random.choice(item)是从当前影视条目(包含标题、类型、类别的列表)中随机选元素,可能选到类型/类别;若误操作传入字符串而非标题列表,还会导致选择单个字符
解决方案
1. 从所有符合条件的标题中随机选择
- 先收集所有符合条件的标题到独立列表,筛选完成后再从该列表中随机选取,而非循环时逐个覆盖Label内容
2. 显示完整标题
- 确保随机选择的对象是标题列表,而非单个影视条目,保证选中的是完整标题字符串
修改后的完整代码
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 = 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(): # 清空文本框之前的内容,避免重复累加 Mood_text.delete(1.0, customtkinter.END) matched_titles = [] keyword = My_Mood.get().strip() for item in movies: # 支持匹配类型(item[1])或类别(item[2]),满足输入"Comedy"或"Show"的需求 if item[1] == keyword or item[2] == keyword: matched_titles.append(item[0]) Mood_text.insert(customtkinter.END, item[0] + "\n") # 根据匹配结果设置Label内容 if matched_titles: random_title = random.choice(matched_titles) Mood_Label.configure(text=random_title) else: Mood_Label.configure(text="未找到匹配内容") def clear(): # 清空Label和文本框内容 Mood_Label.configure(text="A") Mood_text.delete(1.0, customtkinter.END) 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) # 新增清空按钮 Clear_Button = customtkinter.CTkButton(master=Logframe, text="Clear", width=60, height=20, command=clear) Clear_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) root.mainloop()
内容的提问来源于stack exchange,提问作者LaLuna
相关产品推荐
相关产品推荐

