Reputation: 71
I have created a simple Python program, it's my first major one since Python is a new language to myself. It's not polished yet, but it runs decently enough. The goal of the app is to allow the user to get into their file directory and select .exe files that they wish to open up.
The app creates a .txt file and saves these files and then allows the user to select the "run app" button and it will start up those .exe files. I'm stoked that it works right, albeit somewhat sloppy; but now I have an issue.
There is a way to select files and a way to run those .exe files, but I don't know how to make it so that the user can delete the .txt file and restart the list, without having to go into the code to do so.
It's all through a GUI and I would like to keep it that way, is there a way that I can create a way for the user to do this? I appreciate any help you all can give, and if you see something else wrong with it, please let me know; it's my first real plunge into python, so I know I have lots to learn!
I would sort of like a way to create a button that could be pressed and allow the user to enter into that .txt file and remove the .exe file names that they don't want; I just really don't know how to start that at all.
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 24 bold')
splash_label.pack(pady=20)
splash_root.after(3000,splash_root.destroy)
splash_root.mainloop()
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 + ',')
Upvotes: 3
Views: 91
Reputation: 473
If you are trying to delete the contents of a text file, then just open the file and write into it an empty string.
with open('filename.txt') as f:
f.write('')
This will write over everything in the file, and replace it with the empty string, deleting all the contents.
Upvotes: 1