首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python GUI的闪屏

Python GUI的闪屏
EN

Stack Overflow用户
提问于 2020-11-03 02:23:44
回答 1查看 856关注 0票数 0

所以我是python的新手,我一直在尝试创建一个python应用程序来保存.exe文件,然后在你“运行”该应用程序时打开它。在一些youtube教程和这里的一些注释之后,我已经设法做到了这一点;然而,我想在我的应用程序中添加一个闪屏,我正在尝试跟随一些示例,但到目前为止还没有显示闪屏。据我所知,代码应该工作,屏幕本身应该显示,不是它将100%工作,但它应该只是显示。我不确定我做错了什么,我需要一些帮助来弄清楚下一步该做什么;以下是我在尝试实现闪屏之前的代码:

代码语言:javascript
运行
复制
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 + ',')

代码的第二部分是在我尝试实现闪屏时:

代码语言:javascript
运行
复制
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 + ',')

就像我说的,我是一个新的编码风格,所以如果你看到任何其他的错误,或者只是没有意义,请让我知道!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-03 02:37:44

除非您将Tk()窗口放在mainloop()中,否则它不会显示出来,所以它应该是这样的:

代码语言:javascript
运行
复制
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()

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64651158

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档