【问题标题】:Splash Screen for Python GUIPython GUI 的启动画面
【发布时间】:2021-02-15 10:35:14
【问题描述】:

所以我是 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 + ',')

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

【问题讨论】:

  • 我看不到你叫splash_root.mainloop()的任何地方,因为这是Tk(),我认为有必要说mainloop()才能出现窗口
  • @CoolCloud 我会放在:splash_root = Tk() 之后还是之前?
  • 你甚至可以使用一个名为 tksplash 的模块查看 pypi.org 上的文档

标签: python tkinter splash-screen


【解决方案1】:

Tk() 窗口不会显示,除非您将其放在 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() 窗口不需要mainloop(),因为它们使用主窗口的mainloop()

【讨论】:

  • 感谢您的帮助,它现在正在工作;我试图使用的教程向我展示了我必须做的比你发布的更多!如果可以的话,我想问你一些别的问题;我的程序允许用户在他们的文件目录中选择 .exe 文件,一旦程序运行就会打开这些文件。这部分工作正常,但我不确定如何创建一种方法来允许用户删除他们不想再使用的 .exe 文件。
  • @Zethos 使用os.remove(),看看here
猜你喜欢
  • 2013-04-14
  • 2016-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多