0

I code this program using python.... it works fine, but when I run the program and does not do anything or like running and closing it ,,, it displays the directory it is saved on.

any help to solve this ???????

Code starts here>>

#1 import all the libraries
import os
import tkinter.filedialog as fd
import tkinter.messagebox as mb
from tkinter import *

#2 Defining the functions ->>>> four Function

def open_file(): #a function for opening file
    file = fd.askopenfilename(title='choose a file to open',filetypes=[('All files','*.*')])
    os.startfile(file)
def open_folder():
    foldertodlt = fd.askdirectory(title='Select a folder to Open')
    os.startfile(foldertodlt)
def delete_file():
    filetodlt = fd.askopenfilename(title='Select a File to Delete! ',filetypes=[('All files','*.*')])
    os.remove(filetodlt)
    mb.showinfo(title='Deleting File', message='File Deleted! ')
def delete_folder():
    fodlertodlt = fd.askdirectory(title='Select a Folder to Delete! ')
    os.rmdir(fodlertodlt)
    mb.showinfo(title='Deleting Folder ?', message='Folder Deleted')
def list_directories():
    folder = fd.askdirectory(title='Select a Directory to list it\'s files and folders ')
    files = os.listdir(folder)
    listfiles = Toplevel(windowframe)
    listfiles.title(f'Files and Folders in {folder}')
    listfiles.resizable(1,1)
    listbox = Listbox(listfiles, selectbackground='gray', font=('courier',20))
    listbox.place(relx=0,rely=0,relheight=1,relwidth=1)

    i = 0
    while i <len(files):
        listbox.insert(END,files[i])
        i += 1

#3 creating the window .. we must creat an object from the tkinter class and use it

windowframe = Tk()
windowframe.title('File Browser')
windowframe.geometry('300x300')
windowframe.resizable(0,0)
windowframe.config(background='orange')
windowframe.iconbitmap('icon.ico')

#4 creating the buttons to and calling the functions

Button (windowframe, text='Open a File',foreground='white', width=20, font='Courier',background='gray',command=open_file).place(x=50,y=50)
Button (windowframe, text='Open a Folder',foreground='white',width=20, font='Courier',background='gray',command=open_folder).place(x=50,y=100)
Button (windowframe, text='Delete a File',foreground='white',width=20,font='Courier',background='gray',command=delete_file).place(x=50, y= 150)
Button (windowframe, text='Delete a Folder',foreground='white',width=20,font='courier',background='gray',command=delete_folder).place(x=50, y=200)
Button (windowframe, text='List all Directories',foreground='white',width=20,font='courier',background='gray',command=list_directories).place(x=50, y=250)


windowframe.update()
windowframe.mainloop()

End of the Code <<<

  • please answer it ?? it got a project to present.. – Wahid Anwari Jun 18 '22 at 16:06
  • 1
    I can't reproduce any problem with your code. It works as expected except that there is no `os.startfile` on other systems than MS Windows. Check out: https://stackoverflow.com/questions/17317219/is-there-an-platform-independent-equivalent-of-os-startfile for a platform independent replacement for `os.startfile` – Claudio Jun 19 '22 at 03:33

0 Answers0