You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tkinter程序无法显示下拉菜单与复选框问题求助

问题分析与解决方案

核心问题

你的代码存在几个关键遗漏,导致程序运行后直接退出:

  • 未创建主窗口并启动事件循环:tkinter程序必须创建Tk()实例作为主窗口,并且调用mainloop()维持界面运行。
  • 未将WelcomePage框架挂载到主窗口:你定义了WelcomePage类,但没有将其添加到主窗口中显示。
  • 存在未定义的属性引用:代码中self.show.movie_info、self.show.actor_info等调用里的show属性从未定义,会引发AttributeError。

修复后的完整代码

import tkinter as tk
from tkinter import messagebox
from tkinter import StringVar

class WelcomePage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.check_rikkefied()

    def check_rikkefied(self):
        answer = messagebox.askyesno("Rikkefication check", "Are you rikkefied")
        if answer:
            self.create_widgets()
            self.pack(fill="both", expand=True)
        else:
            self.master.destroy()

    def show_movie_info(self, selected):
        messagebox.showinfo("提示", f"选中电影:{selected}")

    def show_actor_info(self, selected):
        messagebox.showinfo("提示", f"选中演员:{selected}")

    def show_country_info(self, selected):
        messagebox.showinfo("提示", f"选中国家:{selected}")

    def create_widgets(self):
        # 创建下拉菜单标签
        title_label = tk.Label(self, text="Title")
        genre_label = tk.Label(self, text="Genre")
        actor_label = tk.Label(self, text="Actor")
        type_label = tk.Label(self, text="Movie / Show")
        country_label = tk.Label(self, text="Country")
        decade_label = tk.Label(self, text="Released in")

        # 创建下拉菜单
        try:
            with open('movie_title.txt') as f:
                movie_titles = [line.strip() for line in f.readlines()]
        except FileNotFoundError:
            movie_titles = ["示例电影1", "示例电影2"]

        title_var = tk.StringVar(self)
        title_var.set("Movie Title")
        title_dropdown = tk.OptionMenu(self, title_var, *movie_titles, command=self.show_movie_info)

        try:
            with open('actors_list.txt') as f:
                actor_list = [line.strip() for line in f.readlines()]
        except FileNotFoundError:
            actor_list = ["示例演员1", "示例演员2"]

        actor_var = tk.StringVar(self)
        actor_var.set("Actor")
        actor_dropdown = tk.OptionMenu(self, actor_var, *actor_list, command=self.show_actor_info)

        genre_var = StringVar(self)
        genre_var.set("Genre")
        genre_dropdown = tk.OptionMenu(self, genre_var, "Action", "Animation","Drama", "Fantasy", "Historical", "Comedy", "War", "Sci-Fi", "Thriller", "Western")

        try:
            with open('country_list.txt') as f:
                country_list = [line.strip() for line in f.readlines()]
        except FileNotFoundError:
            country_list = ["中国", "美国"]

        country_var = tk.StringVar(self)
        country_var.set("Country")
        country_dropdown = tk.OptionMenu(self, country_var, *country_list, command=self.show_country_info)

        decade_var = StringVar(self)
        decade_var.set("Released in ")
        decade_dropdown = tk.OptionMenu(self, decade_var, "1940's", "1950's", "1960's", "1970's", "1980's", "1990's", "2000's", "2020's", "2030's")

        type_var = StringVar(self)
        type_var.set("Movie / TV ")
        type_dropdown = tk.OptionMenu(self, type_var, "Movie", "TV Show")

        # 创建复选框
        checkbox_var = tk.BooleanVar(self)
        checkbox_var.set(False)
        checkbox = tk.Checkbutton(self, text="Rikkefication", variable=checkbox_var)

        # 使用pack添加组件到框架
        title_label.pack()
        title_dropdown.pack()
        genre_label.pack()
        genre_dropdown.pack()
        actor_label.pack()
        actor_dropdown.pack()
        type_label.pack()
        type_dropdown.pack()
        country_label.pack()
        country_dropdown.pack()
        decade_label.pack()
        decade_dropdown.pack()
        checkbox.pack()

# 程序入口
if __name__ == "__main__":
    root = tk.Tk()
    root.title("电影数据库")
    app = WelcomePage(root, root)
    root.mainloop()

关键修复点说明

  • 添加程序入口:新增if __name__ == "__main__"块,创建主窗口、实例化WelcomePage并启动事件循环,这是tkinter程序运行的基础。
  • 挂载框架到主窗口:用户选择"Yes"后,调用self.pack(fill="both", expand=True)将WelcomePage框架显示在主窗口中。
  • 修复未定义方法引用:将self.show.movie_info改为类内定义的self.show_movie_info等方法,避免属性错误。
  • 增加文件异常处理:当txt文件不存在时提供默认选项,防止程序崩溃。
  • 修正拼写错误:将"Westrern"改为正确的"Western"。

内容的提问来源于stack exchange,提问作者bert coertjens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 07:33:19