2

I am writing python program for GUI base for volatility framework, I am still at the initial stage as i am beginner for python, I am using file import from "askopnefilename", the function giving error, when i open and, close without selecting file, So could you help me to solve it.

Code :

from tkinter import *
from tkinter import filedialog
from tkinter.filedialog import askopenfilename
from mtTkinter import *
import subprocess


def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

def OpenFile():
    file = open(askopenfilename(),'r')
    print(root.file)

def Quit():
    root.destroy()

def Test():
    print("Hello world")
    input("Press return to exit")

def Shell():
#    print("Below is the output")
    subprocess.call('./home/shanaka/bash_lerning/function1.sh',shell=True)


root = Tk()
root.title("Volatility")
root.geometry("600x400")

menubar = Menu(root)
startmenu = Menu(menubar, tearoff=0)
startmenu.add_command(label="Import", command=OpenFile)
startmenu.add_separator()
startmenu.add_command(label="Exit", command=Quit)
menubar.add_cascade(label="Start", menu=startmenu)

searchmenu = Menu(menubar, tearoff=0)

submenu = Menu(searchmenu)
submenu.add_command(label="New feed", command=Shell)
submenu.add_command(label="Bookmarks")
submenu.add_command(label="Mail")
searchmenu.add_cascade(label='Plugins', menu=submenu, underline=0)

searchmenu.add_separator()
#searchmenu.add_command(label="Plugins", command=Test)
searchmenu.add_command(label="Copy", command=donothing)
searchmenu.add_command(label="Paste", command=donothing)
searchmenu.add_command(label="Delete", command=donothing)
searchmenu.add_command(label="Select All", command=donothing)
menubar.add_cascade(label="Search", menu=searchmenu)

reportmenu = Menu(menubar, tearoff=0)
reportmenu.add_command(label="Generate Reports", command=donothing)
menubar.add_cascade(label="Reports", menu=reportmenu)


helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=donothing)
helpmenu.add_command(label="About...", command=donothing)
menubar.add_cascade(label="Help", menu=helpmenu)

root.config(menu=menubar)
root.mainloop()

error as below

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1490, in __call__
    return self.func(*args)
  File "/home/shanaka/Project1.py", line 13, in OpenFile
    file = open(askopenfilename(),'r')
FileNotFoundError: [Errno 2] No such file or directory: ''
0

1 Answer 1

6

Open the file only if the askopenfilename returns non-falsy value:

def OpenFile():
    filepath = askopenfilename()
    if filepath:
        file = open(filepath, 'r')
        ...

BTW, the code didn't assigned root.file; accessing root.file will raise an AttributeError.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.