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

如何合并两个tkinter文件为单一程序 实现欢迎页10秒后跳转选择菜单

合并Tkinter欢迎界面与选择菜单界面实现方案

以下是合并后的完整可运行代码,实现欢迎界面展示10秒后自动关闭、跳转至选择菜单的效果:

from tkinter import *
from PIL import ImageTk, Image
from tkinter.ttk import *

def show_menu():
    # 销毁欢迎窗口
    welcome_root.destroy()
    # 初始化选择菜单窗口
    menu_root = Tk()
    menu_root.title("selection")
    menu_root.geometry("325x396")
    menu_root.iconbitmap("left this on purpose")
    
    select_img = ImageTk.PhotoImage(Image.open("left this on purpose"))
    lbl2 = Label(menu_root, image=select_img)
    lbl2.pack()
    # 保留图片引用,防止被垃圾回收导致显示空白
    lbl2.image = select_img

    menu_root.mainloop()

# 初始化欢迎窗口
welcome_root = Tk()
welcome_root.title("-----")
welcome_root.geometry("425x508")
welcome_root.iconbitmap('left this on purpose')

welcome_img = ImageTk.PhotoImage(Image.open("left this on purpose"))
lbl = Label(welcome_root, image=welcome_img)
lbl.pack()

# 10秒后调用跳转函数
welcome_root.after(10000, show_menu)

welcome_root.mainloop()

关键调整说明

  • 移除重复的导入语句,仅保留一次全局导入
  • 对两个窗口实例使用不同的变量名(welcome_root/menu_root),避免变量覆盖冲突
  • 额外绑定了Label控件的image属性到控件实例,避免Python垃圾回收机制清理图片资源导致界面图片不显示
  • 跳转逻辑封装为独立函数,通过after方法10秒后触发,先销毁欢迎窗口再初始化菜单窗口

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 22:00:05