Search code examples
pythonpython-3.xtkinter

Variable was not returning from external function when we use lambda in Button Tkinter


I have written main file as MMM_LaTeX_PreCleanup.py and three external files which have different function namely TeXImport.py, VadidateMMMModule.py and XMLImport.py.

MMM_LaTeX_PreCleanup.py coding was below

import os, sys
import re, io
from tkinter import *
from tkinter import filedialog
from tkinter import messagebox

######### Internal Module
from XMLImport import OpenXML    
from TeXImport import OpenTeX
from VadidateMMMModule import VadidateMMM 


#########################################

MMM = Tk()
MMM.title("MMM TeX Normalization")
MMM.minsize(700,350)

########### XML File Selection
Text1 = Label(MMM,text="Please select your Metadata XML")
Text1.place(x=10,y=20)

XMLFile = Label(MMM,text="XML file was not selected",fg="red")
XMLFile.pack()
XMLFile.place(x=10,y=45)

Browse1 = Button(MMM,text="<Browse>",bg="blue",fg="white",command=lambda: OpenXML(XMLFile)).place(x=220,y=15)
###########################################

############ TeX File Selection
Text2 = Label(MMM,text="Please select your TeX file")
Text2.place(x=10,y=70)

TeXFile = Label(MMM, text="TeX file was not selected",fg="red")
TeXFile.pack()
TeXFile.place(x=10,y=100)


Browse2 = Button(MMM,text="\\Browse", bg="blue",fg="white", command=lambda: OpenTeX(TeXFile)).place(x=180,y=70) ## , font="bold"
#####################################################

########## Output directory

Text3 = Label(MMM,text="Please copy/paste your output LOCAL directory on below box")
Text3.place(x=10,y=130)

name_var = StringVar()
MyLocalPath = Entry(MMM, textvariable=name_var, font=('calibre',10,'normal'),width=75)
##MyLocalPath.pack(ipadx=150,ipady=10)
MyLocalPath.place(x=10,y=160)
##############################################################

############## Journal List
 
Text4 = Label(MMM,text="Select your Journal name")
Text4.place(x=10,y=190)

def callback(selection):
    global GetJournal
    GetJournal = selection
    return GetJournal


JournalList=StringVar(MMM)
JournalList.set("Select your journal")

JournalListDrop = OptionMenu(MMM,JournalList,
                    "AA", "BB", "CC", "DD",command=callback)

JournalListDrop.pack()
JournalListDrop.config(bg="YELLOW")
JournalListDrop.place(x=180,y=190)

SelectedJournal = JournalList.get()
##################################################################


################ Article Number

Text5 = Label(MMM, text="Enter your Article number")
Text5.place(x=350,y=190)

Art_ID = StringVar()
ArticleNo = Entry(MMM,textvariable=Art_ID,font=('calibre',10,'normal'),width=10)
ArticleNo.place(x=500,y=190)
#######################################################################

################### Validate, Clear, and Quit button

Validate = Button(MMM,text="Validate",bg="green",fg="white",bd=3,
                    command=lambda: VadidateMMM(name_var,
                                                Art_ID,
                                                GetJournal,
                                                XMLFileName
                                                )).place(x=10,y=240)
ClearAll = Button(MMM,text="Clear",bg="orange",fg="black",bd=3).place(x=90,y=240)
Exit = Button(MMM,text="Exit",bg="red",fg="white",bd=3,command=sys.exit).place(x=150,y=240)

MMM.mainloop()

XMLImport.py

from tkinter import *
from tkinter import filedialog
from tkinter import messagebox
import io, re, sys, os
import shutil

def OpenXML(XMLFile):
    global XMLFileName
    XMLFileName = filedialog.askopenfilename(initialdir = "/", title="Select your metadata XML file",
                                          filetypes=(("XML File","*.xml"),("all file","*.*")))    
    XMLFile.configure(text="XML File selected: "+XMLFileName,fg='green')    
    return XMLFileName

TeXImport.py

def OpenTeX(TeXFile):
    global TeXFileName
    TeXFileName = filedialog.askopenfilename(initialdir = "/", title="Select your TeX file",
                                          filetypes=(("TeX File","*.tex"),("all file","*.*")))    
    TeXFile.configure(text="TeX File selected: "+TeXFileName,fg='green') 
    return TeXFileName

VadidateMMMModule.py

import os, sys
from tkinter import messagebox
import shutil

def VadidateMMM(name_var,Art_ID,GetJournal,XMLFileName):
    MyLocalPath = name_var.get()
    #### Validating it was only be a local path
    if not MyLocalPath.startswith(("D:","E:","F:","G:","H:")):
        messagebox.showinfo("__file__","Your are set output to local path. Hence i quit")
        sys.exit()    
    ############################################    
    MyArticleNumber = Art_ID.get()
    MyJournal = GetJournal
    
    ###### Creating the Folder
    os.chdir(MyLocalPath)
    os.mkdir(MyJournal+"-"+MyArticleNumber)
    
    ###### Copying the XML file
    shutil.copy(XMLFileName,MyLocalPath)   

XML file was browsed by Button1. Then, XML file was browsed, but XMLFileName variables was not returning to the main file MMM_LaTeX_PreCleanup.py by lambda. TeX file was not called by Button2. Local path and Article ID was given by name_var and Art_ID. Journal name and XML file name was called by GetJournal and XMLFileName Please see my screenshot

enter image description here

Please help how to get the variable XMLFileName when it was in function of another file.


Solution

  • Note that global variables inside a module need to use the module name to access them.

    So suggest to initialize XMLFileName inside XMLImport module:

    # XMLImport.py
    
    XMLFileName = None
    
    def OpenXML(XMLFile):
        global XMLFileName
        ...
    

    And update MMM_LaTeX_PreCleanup as below:

    # MMM_LaTeX_PreCleanup.py
    ...
    import XMLImport
    ...
    
    Browse1 = Button(MMM,text="<Browse>",bg="blue",fg="white",
                        # used module name to call the function
                        command=lambda: XMLImport.OpenXML(XMLFile))
    Browse1.place(x=220, y=15)
    
    ...
    
    Validate = Button(MMM,text="Validate",bg="green",fg="white",bd=3,
                         command=lambda: VadidateMMM("name_var",
                                                     "Art_ID",
                                                     "GetJournal",
                                                     # use module name to access the variable
                                                     XMLImport.XMLFileName
                                                    ))
    Validate.place(x=10, y=240)
    
    ...