As long as you are using Python, you can use the global
keyword when declaring a variable on a local scope. Using it will make your variable goes global and your code will look like this:
def pdf_btnClicked():
global PDFFILE
PathOfPDF = askopenfile()
PDFFILE = PathOfPDF.name
print(PDFFILE)
book = open(PDFFILE, 'rb')
Looking at the code we can inferir that your problem is related to the concept of variable scope.
A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. Basicaly there are two type of variable regarding it's scope:
- Inside a function or a block which is called local variables
In this case, if you declare a varible inside a function, you wont be able to use it outside that function. That's exactly your case: you declared the variable PDFFILE
inside the pdf_btnClicked
function so you will only be able to use the variable inside that. But even so, you are calling the variable outside it. A basic solution for your problem is to so set PDFFILE
as global variable, whose explanation follows bellow.
- Outside of all functions which is called global variables
Global variables, as the name suggest, are global and can be accessed by all function. If you declare PDFFILE
outside the function (and before it) your problem will be solved
Obs.: here we saw the two basics types of variables's scope but it's possible to have more than only two depending on the programming language
Fonts: https://www.tutorialspoint.com/cprogramming/c_scope_rules.htm
https://www.w3schools.com/python/python_scope.asp