Python GUI 的启动画面

Splash Screen for Python GUI

所以我是 python 的新手,一直在尝试创建一个 python 应用程序来保存 .exe 文件,然后在您“运行”该应用程序时打开。在一些 youtube 教程和此处的一些注释之后,我已经设法做到了这一点;但是,我想在我的应用程序中添加启动画面,并且我正在尝试按照一些示例进行操作,但是到目前为止,启动画面没有显示。据我所知,代码应该可以正常工作并且屏幕本身应该可以显示,而不是说它可以 100% 正常工作,但它应该只是显示。我不确定我做错了什么,可以使用一些帮助来确定下一步该做什么;下面是我在尝试实现启动画面之前的代码:

import tkinter as tk
from tkinter import filedialog
import os

root = tk.Tk()
root.title("Start My Apps")
apps = []

if os.path.isfile('save.txt'):
    with open('save.txt', 'r') as f:
        tempApps = f.read()
        tempApps = tempApps.split(',')
        apps = [x for x in tempApps if x.strip()]

def addApp():
    for widget in frame1.winfo_children():
        widget.destroy()

    filename = filedialog.askopenfilename(initialdir = "/", title="Select File", filetypes = (("executables","*.exe"),("all files" , "*")))

    apps.append(filename)
    print(filename)
    for app in apps:
        label1 = tk.Label(frame1, text = app, bg="gray")
        label1.pack()

def runApps():
    for app in apps:
            os.startfile(app)

canvas = tk.Canvas(root, height=700, width=700, bg="#263D42")
canvas.pack(fill="both", expand=True)

frame1 = tk.Frame(root, bg="white")
frame1.place(relwidth = 0.8, relheight = 0.8, relx = 0.1, rely = 0.1)

frame2 = tk.Frame(root, bg="white")
frame2.place(relwidth = 0.8, relheight = 0.05, relx = 0.1, rely = .02)

label2 = tk.Label(frame2, text = "Welcome to: Start My Apps!", font='times 20 bold',bg="white")
label2.pack()

openFile = tk.Button(root, text = "Open File", padx = 10, pady = 5, fg="white", bg="#263D42", command = addApp)
openFile.pack()

runApps = tk.Button(root, text = "Run Apps", padx = 10, pady = 5, fg="white", bg="#263D42", command = runApps)
runApps.pack()

for app in apps:
    label1 = tk.Label(frame1, text = app)
    label1.pack()

root.mainloop()

with open('save.txt', 'w') as f:
    for app in apps:
        f.write(app + ',')

代码的第二部分是我尝试实现启动画面时的代码:

import tkinter as tk
from tkinter import filedialog
from tkinter import *
import os


splash_root = Tk()
splash_root.title("Welcome to: Start My Apps!")
splash_root.geometry("700x700")

splash_label = Label(splash_root, text = "Welcome to: Start My Apps!", font='times 20 bold',bg="white")
splash_label.pack(pady=20)

def main_window():
    root = Tk()
    root.title("Start My Apps")

apps = []

if os.path.isfile('save.txt'):
    with open('save.txt', 'r') as f:
        tempApps = f.read()
        tempApps = tempApps.split(',')
        apps = [x for x in tempApps if x.strip()]

def addApp():
    for widget in frame1.winfo_children():
        widget.destroy()

    filename = filedialog.askopenfilename(initialdir = "/", title="Select File", filetypes = (("executables","*.exe"),("all files" , "*")))

    apps.append(filename)
    print(filename)
    for app in apps:
        label1 = tk.Label(frame1, text = app, bg="gray")
        label1.pack()

def runApps():
    for app in apps:
            os.startfile(app)

canvas = tk.Canvas(height=700, width=700, bg="#263D42")
canvas.pack(fill="both", expand=True)

frame1 = tk.Frame(bg="white")
frame1.place(relwidth = 0.8, relheight = 0.8, relx = 0.1, rely = 0.1)

frame2 = tk.Frame(bg="white")
frame2.place(relwidth = 0.8, relheight = 0.05, relx = 0.1, rely = .02)

label2 = tk.Label(frame2, text = "Welcome to: Start My Apps!", font='times 20 bold',bg="white")
label2.pack()

openFile = tk.Button(text = "Open File", padx = 10, pady = 5, fg="white", bg="#263D42", command = addApp)
openFile.pack()

runApps = tk.Button(text = "Run Apps", padx = 10, pady = 5, fg="white", bg="#263D42", command = runApps)
runApps.pack()

for app in apps:
    label1 = tk.Label(frame1, text = app)
    label1.pack()

mainloop()

with open('save.txt', 'w') as f:
    for app in apps:
        f.write(app + ',')

就像我说的,我是这种编码风格的新手,所以如果您发现其他任何错误或不合理的地方,请告诉我!

A Tk() window 除非你把它放在 mainloop() 中,否则它不会出现,所以它应该是这样的:

splash_root = Tk()
splash_root.title("Welcome to: Start My Apps!")
splash_root.geometry("700x700")

splash_label = Label(
    splash_root, text="Welcome to: Start My Apps!", font='times 20 bold', bg="white")
splash_label.pack(pady=20)

splash_root.after(5000,splash_root.destroy) #after(ms,func)
splash_root.mainloop()

我使用 splash_root.after() 因为它是启动画面,所以必须自动销毁,而不是手动销毁。这将在 5 秒或 5000 毫秒后关闭初始屏幕。

这也可以解释为什么 Toplevel() windows 不需要 mainloop() 因为他们使用主要 window.[=17= 的 mainloop() ]