-1

This is probably a newbie question but I'm having issues figuring out how to get a value from a function, to be used as input in another. I would also like to have some tips about code organization. In this example I'm trying to get the filePath and outPut Folder path to be used in the function processFile.

Thank you

Code:

    from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader

version = "v0.0.01"


root = Tk()



def window (main):

    main.title("File Opener " + version)
    width = main.winfo_width()
    main.geometry('500x200')

numero = str(1)

def OpenFile():
    fileName = askopenfilename(initialdir="C:/",
                            filetypes =(("PDF File", "*.pdf"),("All Files","*.*")),
                            title = "Select PDF File",
                           )

    labelName = Label(text="File Path: " + fileName)
    labelName.pack()
    print(fileName)
    return fileName

def outputFolder():                                         #Bug, label keeps adding paths
    outPath = askdirectory()

    labelName2 = Label(text="Output Folder: " + outPath)
    labelName2.pack()
    print(outPath)
    return outPath



def processFile(inFile,outFolder):

   ''' print(fileName) get input name and output variables
    print(outPath)'''




label = ttk.Label(root, text ="",foreground="black",font=("Helvetica", 16))
label.pack()


#Button Open-----------

button1 = Button (text = "Open File", command = OpenFile)
button1.pack()


#Button Start---------
buttonStart = Button (text = "Start Process", command = processFile)#give as parameter inputFile and link
buttonStart.place(height=50, width=100)

#Button Open-----------

button3 = Button (text = "Output Folder", command = outputFolder)
button3.pack()


#Menu Bar ----------------

menu = Menu(root)
root.config(menu=menu)

file = Menu(menu)


file.add_command(label = 'Open', command = OpenFile)
file.add_command(label = 'Exit', command = lambda:exit())

menu.add_cascade(label = 'File', menu = file)


window(root)
root.mainloop()
dev_jazz
  • 1
  • 3
  • Do not put an image of code. Use the [edit] button with the `{}` to insert code in the question - this allows others to test and improve your code for an answer. – Brian Tompsett - 汤莱恩 Jan 24 '19 at 09:42
  • 1
    Code organization might be a too broad scope for this question. There are other questions with great answers about that (https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application). As for your question about getting a value from a function - you decide the return value of a function when you define it. You can store the return value to variable 'a' by writing 'a = Function()'. – Yoda Jan 24 '19 at 09:42

3 Answers3

1

If you use the function in a variable the returned result from the function is stored in the variable.

def Function1(x):
    return x*2

var1 = Function1(5)

Then var1 = 10.

But about your buttons, do you want to take input from the user that will be saved when the button is pressed or do you want the button to send a set value?

Hemme02
  • 34
  • 1
  • 7
  • Edited my answer. – Hemme02 Jan 24 '19 at 10:30
  • When button1 is clicked it starts function1, and then function gets the filepath of the file the user chose (dialog window). Now, this value, I want to use as input for the functionProcessing that will be also launched when the buttonStart is clicked. – dev_jazz Jan 24 '19 at 10:44
  • Theres probably a better way then this but i would use the function1 to change the value of a global variabel. And then you use this variabel with functionProcessing. [Global_variable](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – Hemme02 Jan 24 '19 at 10:55
  • Making it global would be a nice first step. But when I declare global filename and then assign fileName to the dialog window, the fileName still doesn't exist outside the function...it's still not a recognizer variable outside that scope... – dev_jazz Jan 24 '19 at 11:05
  • Do you declare the variabel as global in the function? – Hemme02 Jan 24 '19 at 11:07
  • yes, i declared inside the function. Like in the thread link you added. Def openFile(): global fileName – dev_jazz Jan 24 '19 at 11:16
  • if I do this print(OpenFile()) I do get an answer. But not in the command button – dev_jazz Jan 24 '19 at 11:23
  • Just to be clear, have you added the global variable outside the function. And when you call on the command_function do you use the variable as an argument? – Hemme02 Jan 24 '19 at 11:32
  • When I declare de variable outside the function it does exist globally (the name is recognized). But not the value from the return of the funcion OpenFile. Anytime I try to use this variable a compilation error (fileName is not defined) – dev_jazz Jan 24 '19 at 11:41
  • So the function openfile():global filename. And then in the function you declare filename = chosen_value. – Hemme02 Jan 24 '19 at 11:53
  • exactly. openfile(): global filename then filename = askopenfilename (initialdir="C:/", filetypes =(("PDF File", "*.pdf"),("All Files","*.*")), title = "Select PDF File", ) – dev_jazz Jan 24 '19 at 11:55
  • now I will need this fileName to use as a parameter inside other function. but its not recognized – dev_jazz Jan 24 '19 at 11:56
  • And does askopenfilename() return a value? – Hemme02 Jan 24 '19 at 11:57
  • yes, when I use the print value inside that function, it does print the path – dev_jazz Jan 24 '19 at 11:59
  • Ok, so the function askopenfilename() returns a value you can print inside the openfile function. – Hemme02 Jan 24 '19 at 12:01
  • yes. how can i pass this value to another function triggered my a button command? – dev_jazz Jan 24 '19 at 13:01
  • Could you answer your own post with your new code so i can look at it. – Hemme02 Jan 24 '19 at 13:34
  • Yes. You can see it bellow. Thank you – dev_jazz Jan 24 '19 at 14:40
0
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader

version = "v0.0.01"


root = Tk()




def window (main):

    main.title("File Opener " + version)
    width = main.winfo_width()
    main.geometry('500x200')
    main.configure(background = 'black')

numero = str(1)

#openFile
def OpenFile():
    global fileName
    fileName = askopenfilename(initialdir="C:/",
                            filetypes =(("PDF File", "*.pdf"),("All Files","*.*")),
                            title = "Select PDF File",
                           )

    labelName = Label(text="File Path: " + fileName)
    labelName.pack()
    print(fileName)
    return str(fileName)

#getOutputPath

def outputFolder():                                         #Bug, label keeps adding paths
    outPath = askdirectory()

    labelName2 = Label(text="Output Folder: " + outPath)
    labelName2.pack()
    print(outPath)
    return outPath

#testFunct

def printFilename(inArg):
    print(inArg)
    x = OpenFile()
    print (OpenFile())




lambda: printFilename(fileName)

#processRealDeal

def processFile(): #THIS IS THE MAIN FUNC

   ''' print(fileName) get input name and output variables
    print(outPath)'''


print (OpenFile)

label = ttk.Label(root, text ="",foreground="black",font=("Helvetica", 16))
label.pack()


#Button Open-----------

button1 = Button (text = "Open File", command = OpenFile)
button1.pack()


#Button Start---------
buttonStart = Button (text = "Start Process", command = lambda: printFilename("Hello World!!!"))#give as parameter inputFile and link
buttonStart.place(height=50, width=100)

#Button Open-----------

button3 = Button (text = "Output Folder", command = outputFolder)
button3.pack()


#Menu Bar ----------------

menu = Menu(root)
root.config(menu=menu)

file = Menu(menu)


file.add_command(label = 'Open', command = OpenFile)
file.add_command(label = 'Exit', command = lambda:exit())

menu.add_cascade(label = 'File', menu = file)


window(root)
root.mainloop()
dev_jazz
  • 1
  • 3
0

I cant see that you have declared the global fileName variabel outside the function.

I did this

from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import PyPDF2
from PyPDF2 import PdfFileWriter, PdfFileReader

version = "v0.0.01"


root = Tk()


fileName = "test"

def window (main):

    main.title("File Opener " + version)
    width = main.winfo_width()
    main.geometry('500x200')
    main.configure(background = 'black')

numero = str(1)

#openFile
def OpenFile():
    global fileName
    fileName = askopenfilename(initialdir="C:/",
                            filetypes =(("PDF File", "*.pdf"),("All Files","*.*")),
                            title = "Select PDF File",
                           )

    labelName = Label(text="File Path: " + fileName)
    labelName.pack()
    test()
    return str(fileName)

#getOutputPath

def test():
    print(fileName)

def outputFolder():                                         #Bug, label keeps adding paths
    outPath = askdirectory()

    labelName2 = Label(text="Output Folder: " + outPath)
    labelName2.pack()
    print(outPath)
    return outPath

#testFunct

def printFilename(inArg):
    print(inArg)
    x = OpenFile()
    print (OpenFile())




lambda: printFilename(fileName)

#processRealDeal

def processFile(): #THIS IS THE MAIN FUNC

   ''' print(fileName) get input name and output variables
    print(outPath)'''


print (OpenFile)

label = ttk.Label(root, text ="",foreground="black",font=("Helvetica", 16))
label.pack()


#Button Open-----------

button1 = Button (text = "Open File", command = OpenFile)
button1.pack()


#Button Start---------
buttonStart = Button (text = "Start Process", command = lambda: printFilename("Hello World!!!"))#give as parameter inputFile and link
buttonStart.place(height=50, width=100)

#Button Open-----------

button3 = Button (text = "Output Folder", command = outputFolder)
button3.pack()


#Menu Bar ----------------

menu = Menu(root)
root.config(menu=menu)

file = Menu(menu)


file.add_command(label = 'Open', command = OpenFile)
file.add_command(label = 'Exit', command = lambda:exit())

menu.add_cascade(label = 'File', menu = file)


window(root)
root.mainloop()

And the test function prints the chosen file. So the global variable is now containing the correct value.

So just declare it at the top and you will be able to retrieve the value

Hemme02
  • 34
  • 1
  • 7
  • Can you share the full code with your alterations? I'm having a different outcome. – dev_jazz Jan 24 '19 at 15:09
  • Edited the response with the full code. To be clear, what works is retrieving the filename with the open file button. It then prints the file path in the terminal. I´ve havent looked at the function for the other buttons. – Hemme02 Jan 25 '19 at 07:02
  • Thank you, but not really OK. Becau now, when I click the start process botton it opens the dialog box, it isn't returning the value already acquired in the botton1(that happens the file dialog and gets the file path) – dev_jazz Jan 25 '19 at 14:26