Python GUI的启动屏幕

2024-09-26 22:44:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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

就像我说的,我对这种编码方式还不熟悉,所以如果你认为还有什么地方是错误的,或者根本没有意义,请告诉我


Tags: appstextinappforrootpacktk
1条回答
网友
1楼 · 发布于 2024-09-26 22:44:50

除非将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()

相关问题 更多 >

    热门问题