【发布时间】: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